When should I make a closure mut? - rust

Let's say I have this code:
let mut s = "hi".to_string();
let c = || s.push_str(" yo");
c();
It doesn't compile and generates this error:
error[E0596]: cannot borrow `c` as mutable, as it is not declared as mutable
--> src\test.rs:120:9
|
119 | let c = || s.push_str(" yo");
| - - calling `c` requires mutable binding due to mutable borrow of `s`
| |
| help: consider changing this to be mutable: `mut c`
120 | c();
| ^ cannot borrow as mutable
For more information about this error, try `rustc --explain E0596`.
error: could not compile `test` due to previous error
Why c(); cannot borrow as mutable? It cannot borrow what as mutable? c? In order to make it work, I have to change it to:
let mut s = "hi".to_string();
let mut c = || s.push_str(" yo");
c();
But here I'm just defining a closure c. I'll not modify it, like c = || x;. Why must define it as let mut c?

But here I'm just defining a closure c. I'll not modify it, like c = || x;. Why must define it as let mut c?
Because a closure is fundamentally a structure, each captured item being a member of that structure.
So your code is roughly equivalent to this:
struct S<'a>(&'a mut String);
impl S<'_> {
fn call(&mut self) {
self.0.push_str("yo");
}
}
fn main() {
let mut s = "hi".to_string();
let c = S(&mut s);
c.call();
}
And that fails to compile with more or less the same error:
error[E0596]: cannot borrow `c` as mutable, as it is not declared as mutable
--> src/main.rs:14:5
|
13 | let c = S(&mut s);
| - help: consider changing this to be mutable: `mut c`
14 | c.call();
| ^^^^^^^^ cannot borrow as mutable
Now you might object that that's because I defined fn call(&mut self), but if you don't you get the internal part of the same error:
error[E0596]: cannot borrow `*self.0` as mutable, as it is behind a `&` reference
--> src/main.rs:8:9
|
7 | fn call(&self) {
| ----- help: consider changing this to be a mutable reference: `&mut self`
8 | self.0.push_str("yo");
| ^^^^^^^^^^^^^^^^^^^^^ `self` is a `&` reference, so the data it refers to cannot be borrowed as mutable
That is you can't modify an &mut through an &. Because if you did you could create multiple &s to the same &mut through which you could modify the pointee, and that would essentially give you multiple &mut.
Which is not allowed by Rust's semantics:
At any given time, you can have either one mutable reference or any number of immutable references.

It may not look like c needs to be mutable because you aren't modifying c itself, but for closure types, the mut keyword is what allows it to mutate any external state. It changes the type of the closure to be a FnMut, which the docs refer to as "a mutably capturing closure".

It's about ownership. mut here does not mean that you will mutate c, but that you require mut-level access to the closure in order to execute it.
c borrows s mutably. This should be fairly obvious, because calling c() modifies s.
Now imagine if c would be usable without mut:
let mut s = "hi".to_string();
let c = || s.push_str(" yo");
let c1 = &c;
let c2 = &c;
c1();
c2();
Here, both c1 and c2 would have mutable access to s simultaneously, which is not possible according to Rusts borrowing rules. This is prevented automatically by the compiler, as it changes the type of the closure to FnMut as soon as you access a variable mutably from within the closure.
Then, you need mut access to the closure to execute it and the case becomes a compiler error:
let mut s = "hi".to_string();
let mut c = || s.push_str(" yo");
let c1 = &mut c;
let c2 = &mut c;
c1();
c2();
error[E0499]: cannot borrow `c` as mutable more than once at a time
--> src/main.rs:5:10
|
4 | let c1 = &mut c;
| ------ first mutable borrow occurs here
5 | let c2 = &mut c;
| ^^^^^^ second mutable borrow occurs here
6 |
7 | c1();
| -- first borrow later used here

Related

rustlings move_semantics2 why passing reference doest not work?

i am trying Rust by doing the restlings exercices that is a very good approach to start with but there is something that i do not understand.
Exercice: move_semantics2
I understand that in order to initialize vec1 with the content of vec0 without taking the ownership away we have to either clone vec0 or to pass by reference. The issue is that this code, that passes references doesn't seem to work.
I wanna understand why, any idea ?
// move_semantics2.rs
// Make me compile without changing line 13 or moving line 10!
// Execute `rustlings hint move_semantics2` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
fn main() {
let vec0 = Vec::new();
let mut vec1 = fill_vec(&vec0);
// Do not change the following line!
println!("{} has length {} content `{:?}`", "vec0", vec0.len(), vec0);
vec1.push(88);
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
}
fn fill_vec(vec: &Vec<i32>) -> &Vec<i32> {
let mut vec = vec;
vec.push(22);
vec.push(44);
vec.push(66);
vec
}
this is the error i am getting :
⚠️ Compiling of exercises/move_semantics/move_semantics2.rs failed! Please try again. Here's the output:
warning: variable does not need to be mutable
--> exercises/move_semantics/move_semantics2.rs:8:9
|
8 | let mut vec1 = fill_vec(&vec0);
| ----^^^^
| |
| help: remove this `mut`
|
= note: `#[warn(unused_mut)]` on by default
error[E0596]: cannot borrow `*vec1` as mutable, as it is behind a `&` reference
--> exercises/move_semantics/move_semantics2.rs:13:5
|
8 | let mut vec1 = fill_vec(&vec0);
| -------- consider changing this binding's type to be: `&mut Vec<i32>`
...
13 | vec1.push(88);
| ^^^^^^^^^^^^^ `vec1` is a `&` reference, so the data it refers to cannot be borrowed as mutable
warning: variable does not need to be mutable
--> exercises/move_semantics/move_semantics2.rs:19:9
|
19 | let mut vec = vec;
| ----^^^
| |
| help: remove this `mut`
error[E0596]: cannot borrow `*vec` as mutable, as it is behind a `&` reference
--> exercises/move_semantics/move_semantics2.rs:21:5
|
19 | let mut vec = vec;
| ------- consider changing this binding's type to be: `&mut Vec<i32>`
20 |
21 | vec.push(22);
| ^^^^^^^^^^^^ `vec` is a `&` reference, so the data it refers to cannot be borrowed as mutable
error[E0596]: cannot borrow `*vec` as mutable, as it is behind a `&` reference
--> exercises/move_semantics/move_semantics2.rs:22:5
|
19 | let mut vec = vec;
| ------- consider changing this binding's type to be: `&mut Vec<i32>`
...
22 | vec.push(44);
| ^^^^^^^^^^^^ `vec` is a `&` reference, so the data it refers to cannot be borrowed as mutable
error[E0596]: cannot borrow `*vec` as mutable, as it is behind a `&` reference
--> exercises/move_semantics/move_semantics2.rs:23:5
|
19 | let mut vec = vec;
| ------- consider changing this binding's type to be: `&mut Vec<i32>`
...
23 | vec.push(66);
| ^^^^^^^^^^^^ `vec` is a `&` reference, so the data it refers to cannot be borrowed as mutable
error: aborting due to 4 previous errors; 2 warnings emitted
For more information about this error, try `rustc --explain E0596`.
I think your misconception stems from this line in fill_vec():
let mut vec = vec;
That line is not making a copy of the original vector, instead it tries to make the original immutable reference to the passed in vector mutable, which naturally the compiler does not allow. If your intent was to make a copy of the the passed-in vector, you would want your function to look like this:
fn fill_vec(vec: &Vec<i32>) -> Vec<i32> {
let mut new_vec = vec.clone();
new_vec.push(22);
new_vec.push(44);
new_vec.push(66);
new_vec
}
Note that this new version uses clone() to copy the original vector, and instead of returning a reference to a vector, it returns a new vector.

Rust - Why does dereferencing and borrowing break borrowing rules?

As per my understanding, there can be multiple immutable references at a time but if there is a mutable reference it can be the only usable reference.
Why does the following code work?:
fn main() {
let mut y = String::from("bar");
let f: &mut String = &mut y;
let f2: &String = &(*f);
// not allowed since mutable reference already exists
// let f3: &String = &y;
println!("{}, ", f.as_str());
println!("{}", f2.as_str());
}
Edit: Another part of my question which I guess isn't obvious is: why am I not allowed to create f3 (like I am doing in the commented line) when it is exactly the same thing as f2 and created similarly by refrencing y.
Because the compiler is smart enough to know if the data actually is used as mutable before it is used as immutable. If you change the code at all to use the mutable reference first, it fails.
fn main() {
let mut y = String::from("bar");
let f: &mut String = &mut y;
let f2: &String = &(*f);
f.clear();
// not allowed since mutable reference already exists
// let f3: &String = &y;
println!("{}, ", f.as_str());
println!("{}", f2.as_str());
}
Here's a link to a live example. The error, as you'd expect, mentions you cannot have an immutable reference if a mutable one exists.
error[E0502]: cannot borrow `*f` as mutable because it is also borrowed as immutable
--> src/main.rs:6:5
|
5 | let f2: &String = &(*f);
| ----- immutable borrow occurs here
6 | f.clear();
| ^^^^^^^^^ mutable borrow occurs here
...
12 | println!("{}", f2.as_str());
| ----------- immutable borrow later used here
For more information about this error, try `rustc --explain E0502`.
error: could not compile `playground` due to previous error
In short, you can only have one borrow if you have a mutable reference, but the compiler is intelligent enough to know when the value cannot change: in your example, f cannot change when it is used before f2 is used, so it knows it doesn't change.

Mutate an object which was taken from a vector

I have trouble solving an obvious simple problem.
Basically I want to push an instance of a structure to a vector to get it out later and to modify the object by calling a function implemented for the structure.
To simplify the case, I created the following test code, which reflects in my opinion to the same problem.
let mut strings = Vec::new();
strings.push("Hello".to_string());
let string_option = strings.last();
let string = string_option.unwrap();
string.shrink_to(1);
This has the compile error
error[E0596]: cannot borrow `*string` as mutable, as it is behind a `&` reference
--> src/main.rs:89:5
|
88 | let string = string_option.unwrap();
| ------ help: consider changing this to be a mutable reference: `&mut String`
89 | string.shrink_to(1);
| ^^^^^^^^^^^^^^^^^^^ `string` is a `&` reference, so the data it refers to cannot be borrowed as mutable
Then I tried sheer endless variants like
let mut strings = Vec::new();
strings.push("Hello".to_string());
let string_option = strings.last().as_mut();
let string = string_option.unwrap();
string.shrink_to(1);
... or ...
let mut strings = Vec::new();
strings.push("Hello".to_string());
let string_option = strings.last().as_deref_mut();
let string = string_option.unwrap();
string.shrink_to(1);
Actually the code shown above is a simplification from this code, which I originally wanted to do.
struct Bar {
data: Vec<String>
}
impl Bar {
fn shrink_first(&mut self) {
let s_opt = self.data.last().as_mut(); // s_opt is of type Option<&mut &String>
let s = s_opt.unwrap(); // s is of type &mut & String
s.shrink_to(1);
}
}
The code above brings the following errors ...
error[E0716]: temporary value dropped while borrowed
--> src/main.rs:67:21
|
67 | let s_opt = self.data.last().as_mut(); // s_opt is of type Option<&mut &String>
| ^^^^^^^^^^^^^^^^ - temporary value is freed at the end of this statement
| |
| creates a temporary which is freed while still in use
68 |
69 | let s = s_opt.unwrap(); // s is of type &mut & String
| ----- borrow later used here
|
= note: consider using a `let` binding to create a longer lived value
error[E0596]: cannot borrow `**s` as mutable, as it is behind a `&` reference
--> src/main.rs:70:9
|
70 | s.shrink_to(1);
| ^^^^^^^^^^^^^^ cannot borrow as mutable
But in my opinion it always has the same root causes, but I have not figured out what to do.
Simply change strings.last() to strings.last_mut().
The last() method returns a standard (immutable) reference (to be more precise, Option<&T>).
To be able to mutate the last String in the vector, you need to get a mutable reference via last_mut().

Why does Rust compiler not detecting an unused-borrowed reference?

Why does the following fail to compile (2018 edition)
fn main() {
let mut s = String::from("hello");
let r1 = &mut s;
{
let _r2 = &s;
}
println!("{}", r1);
}
Why the compiler fails to notice that _r2 is unused and successfully compile the above?
It complains that we have both mutable and immutable refs and mutable is used in println.
the confusing part is that literature (books etc...) talk about "using block to restrict borrowing scope. "Beginning Rust" p348 for example. So I found it confusing that we advocate for such practices but it (sometimes) does not work.
But it does work here; the scope of _r2 is restricted by the block it's
declared in. The problem is just that r1 is visible in this scope as well.
That is, the error message is not saying "you can't println because there are both mutable and immutable borrows at that point", but "you can't declare _r2 because there is already a mutable borrow at that point". println only appears as a reason why r1 is still alive at the point of _r2's declaration.
You can see it from the error message:
error[E0502]: cannot borrow `s` as immutable because it is also borrowed as mutable
--> src/main.rs:5:19
|
3 | let r1 = &mut s;
| ------ mutable borrow occurs here
4 | {
5 | let _r2 = &s;
| ^^ immutable borrow occurs here
6 | }
7 | println!("{}", r1);
| -- mutable borrow later used here
^^ points at the actual error location; -- are other related locations.
You can only do like this,
fn main(){
let mut s = String::from("hello");
let r1 = &mut s;
{
let r2 = &r1;
}
println!("{}", r1);
}

Does a generic lifetime materialize as the reference's lifetime or the referenced value's lifetime?

Consider the following program:
fn main() {
let c; |<-'a
let mut a = "Hello, world!".to_string(); |
{ |
let b = &mut a; |<-'b |
c = foo(b); | |
} |
println!("{}", c) |
}
fn foo<'z>(a: &'z mut str) -> &'z str {
a
}
b's lifetime is 'b, but c's lifetime is 'a, which is longer than 'b. foo's lifetime constraint says foo's return value (c in this case) should have the same lifetime with its argument (b in this case). How is foo's lifetime constraint satisfied?
However, this program compiles, so I guess foo's lifetime parameter 'z materializes as b's referenced value (a)'s lifetime so that foo's lifetime constraint is satisfied?
A value has a lifetime of its own, but a reference also tracks the lifetime of the thing it references. Unfortunately, there's a lack of official terminology to use here. The term that I (and some others) have started using is concrete lifetime. There are three variables in main and thus there are three concrete lifetimes:
fn main() {
let c; // 'c
let mut a = String::new(); // 'a ¦
{ // | ¦
let b = &mut a; // | 'b ¦
c = foo(b); // | | |
} // | |
println!("{}", c) // | |
}
a is a String, b is a &mut String, and c is a &str. All three variables are values, but b and c are also references. In this case, b refers to the value in a and is &'a mut String. Since c is derived from b, it has the same "inner lifetime": &'a str.
Notably, the lifetime of b itself never comes into play. It's exceedingly rare for it to, as you need to have mutable borrows and an "extra" borrow:
fn main() {
let c;
let mut a = String::new();
{
let mut b = &mut a;
c = foo(&mut b); // Mutably borrowing `b` here
}
println!("{}", c)
}
error[E0597]: `b` does not live long enough
--> src/main.rs:6:17
|
6 | c = foo(&mut b);
| ^^^^^^ borrowed value does not live long enough
7 | }
| - `b` dropped here while still borrowed
8 | println!("{}", c)
| - borrow later used here
In this case, the value passed to foo is of type &'b mut &'a mut String, which is coerced down to &'b mut str. The value b does not live long enough, and you get the error.
I don't think this model can account for more complicated borrowing relationships. If a is used again after the println!, for example, the mutable borrow can't be for the entire lifetime of a
The mutable borrow of a is held by c, but the duration of the borrow doesn't need to correspond to the lifetime of c. Due to non-lexical lifetimes (better called "non-lexical borrows" in this context), the borrow of a held by c can terminate after the println! but before the end of scope.
Enhancing the the diagram from above to show the lifetime of the value combined with the lifetime of the referred-to value in parenthesis:
fn main() {
let c; // 'c
let mut a = String::new(); // 'a ¦
{ // | ¦
let b = &mut a; // | 'b('a) ¦
c = foo(b); // | |('a) |('a)
} // | |('a)
println!("{}", c); // | |('a)
// | |
println!("{}", a); // | |
}
See also:
Why does linking lifetimes matter only with mutable references?
How can this instance seemingly outlive its own parameter lifetime?
Mutable borrow in a getter not living long enough
Reference does not live long enough in nested structure
Cannot borrow as mutable more than once at a time in one code - but can in another very similar
What is the difference between '&self' and '&'a self'?

Resources