Are reference values copied in Rust? [duplicate] - reference

This question already has answers here:
Copy/move semantics documentation of &T/&mut T types itself
(2 answers)
Closed 5 years ago.
Am I correct to assume that, for the following code
let a = vec![1, 2, 3];
let b = &a;
let c = b;
The memory presentation will be something like this, assuming the value of b is "B"?
_ _
b|B| c|B|
|____________|
|
V
_________
a|_________|
I'm only asking about immutable references, as there can be only 1 mutable reference, as far as I remember.

Yes, this is correct.
In Rust terms, &T is Copy, which means that it can be copied bitwise without transferring ownership.

Related

Does the compiler make dereference mutable reference and then re-reference when passed to a function with a reference parameter? [duplicate]

This question already has answers here:
Confusion over auto-dereferencing rules when using function
(1 answer)
What are Rust's exact auto-dereferencing rules?
(4 answers)
Closed last month.
Let's say I have such a function:
fn take_by_ref(i:&i32){
println!("{}",i);
}
Now I want to pass mutable reference there:
#[test]
fn test_mut(){
let mut x = 9;
let m = &mut x;
}
And it is unclear what is the difference between the two calls to this function?
first:
take_by_ref(m);
second:
take_by_ref(&*m);
Are these two calls equal and the compiler makes first call as the second one automatically? or is there a difference?
And won't the rule that either mut reference or immutable can exist be violated here when call take_by_ref?
That is, the question is that when passing a mut variable to such a function, it can become auto-immutable?

How to assign to a slice range? [duplicate]

This question already has answers here:
How do you copy between arrays of different sizes in Rust?
(3 answers)
Closed 1 year ago.
I have a mutable slice, and I want to replace its prefix if it equals a certain slice.
I tried this:
let len = prefix.len();
if slice.starts_with(prefix) {
slice[.. len - 1] = subst;
}
Playground
However,
error[E0277]: the size for values of type `[{integer}]` cannot be known at compilation time
--> src/main.rs:13:9
|
13 | slice[.. len - 1] = *subst;
| ^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
(Let's forget about the other type error in the playground; in my code, I have proper slices instead of arrays.)
I understand this as saying "The slices might have different length, so you cannot do this".
However, how can I do this when I explicitly checked that the lengths are the same?
You should try split_at_mut(), something like this:
let len = prefix.len();
if slice.starts_with(prefix) {
let (left, _right) = slice.split_at_mut(len);
left.copy_from_slice(subst);
}
There is also explanation about this specific problem here: copy_from_slice()
Edit:
As Jmb stated in a comment, split_at_mut() is not needed:
let len = prefix.len();
if slice.starts_with(prefix) {
slice[..len].copy_from_slice(subst);
}

Is this a grammar issue or am I understanding it wrong? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I'm reading the book Programming Rust. There are some places I don't quite understand.
Example 1
For this code
fn main() {
let r;
{
let x = 1;
r = &x;
}
println!("{:?}", r);
}
The author states
In this example, there are three lifetimes whose relationships we need to work out. The variables r and x each have a lifetime, extending from the point at which they’re initialized until the point that they go out of scope. The third lifetime is that of a reference type: the type of the reference we borrow to &x, and store in r.
Example 2
For this code
let v = vec![1, 2, 3];
let r = &v[1];
The author states
These rules apply in a natural way when you borrow a reference to some part of some larger data structure, like an element of a vector:
Example 3
You can “borrow a reference” to a value; references are nonowning pointers, with limited lifetimes.
Note here the quotes are in the text of the book, not added by me.
Note the bold text in the description of the two examples. Why the author always say borrow sth to sth instead of borrow sth from sth? I'm not a native English speaker and as I understand borrow sth from sth/sb is the correct usage. When talking about Rust's reference borrowing, is this usage correct?

Dereference a closure parameter [duplicate]

This question already has answers here:
Meaning of '&variable' in arguments/patterns
(1 answer)
What's the difference between ref and & when assigning a variable from a reference?
(3 answers)
How do references work in patterns in binding expressions? [duplicate]
(1 answer)
Closed 3 years ago.
The title might not be appropriate, be here is an example:
fn foo(impl Fn(&u32) -> bool) { ... }
foo(|x| *x < 100);
foo(|&x| x < 100);
Are the two closures passed to foo equivalent? I saw people use the second form in some places, but I couldn't find it in the official book. Is the &x part a destructure...?
This is called reference pattern:
ReferencePattern :
(&|&&) mut? Pattern
Reference patterns dereference the pointers that are being matched and, thus, borrow them.
So yes, they are equivalent.

rust custom indexable data types [duplicate]

This question already has answers here:
Is there a way to perform an index access to an instance of a struct?
(2 answers)
Closed 4 years ago.
Is it possible to create custom data types in rust that can be indexed like Vec and Slice? If yes which Traits do I need to implement?
Example for indexed access:
let mut vec = vec![1, 2, 3];
assert_eq!(vec[1], 2);
Yes, the traits are called Index and IndexMut.

Resources