Is there a difference between a borrowed integer and a copy?

Susp1cious

I know that a String mainly consists of a pointer that contains the address to its allocated place in the heap memory. Rust prohibits any copies of Strings to avoid double free errors, so it introduced borrowing, where the code basically only copies the pointer value without copying the value in the heap.

However, integer types are stored in the stack and hence do not have a pointer. Yet it is still possible to create a reference to an integer:

let i: i64 = 42;
let j = &i;

Since an integer contains no reference to the heap, isn't a borrowed integer simply a regular copy of it? E.g. is there any difference between j = i and j = &i?

kmdreko

Consider the case if the reference were mutable:

let mut i: u64 = 42;
let j = &mut i;

*j = 5;
println!("{}", i);
5

It should be obvious from this demonstration that j is not simply a copy. It does reference and therefore modify the original i.

Integer types are stored in the stack and hence do not have a pointer.

Not sure where you got that idea from. If it exists in memory, then it has an address within that memory, and therefore you can have a pointer (or reference) that points to it. The properties of a u64 do not change depending on where it is.

The comparison to strings may be tripping you up:

let s = String::from("hello world");
let s_ref: &String = &s;
let str_ref: &str = s.as_str();

If you have a String variable s, and take a reference to it, s_ref, it does not point directly to the heap, it points to the variable s on the stack. There is a slice-type str that represents a region of utf8-encoded bytes, which a String holds on the heap. You can get a reference to that region of memory directly on the heap by getting it via .as_str()/.as_ref() or by converting the &String into a &str via deref coercion.

But in the case of u64 vs &u64, there isn't much of a practical difference between the two except the latter incurs an extra level of indirection in the generated code and you may have to worry about lifetime constraints. Because of that, its usually better to use copies of integer types if given the choice. You'd still see references to integers though if using them through some generic interface.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to find difference between two integer arrays?

What is the difference between Integer and int in Java?

What is the difference between Copy and Clone?

What is the difference between Int and Integer?

What is the difference between a deep copy and a shallow copy?

Difference between PLS_INTEGER and INTEGER for below scenario

Difference between INSERT and COPY

Difference between iter() and into_iter() on a shared, borrowed Vec?

DockerFile : Difference between ADD and COPY

difference between int and Integer type in groovy

Laravel validation : difference between numeric and integer?

What is the difference between Int and Integer in Kotlin?

Any difference between IS INITIAL and = 0 for integer?

Difference between memcpy and copy by assignment

Difference between copy and \copy commands in postgresql

difference between integer <<32 and <<31<<1

Convert the difference between two dates into an integer

Logic using the difference between two integer variables

What is the difference between normalized, scaled and integer VkFormats?

What is the difference between copy and deep copy in Julia?

Difference between writing an integer in a HEX from a real

Is any difference between bitwise copy and shallow copy?

Difference between shallow and deep copy

Difference between LongInt and Integer, LongWord and Cardinal

difference between string array and integer array

Find set difference between two integer intervals

difference between character array and integer array

working with datetime: get the difference between dates as an integer

In ClojureScript, What is the difference between `int?` and `integer?` and `number?`?