You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Compiling playground v0.0.1 (/playground)
error[E0382]: use of moved value: `a`
--> src/lib.rs:8:13
|
7 | let _ = |a| for _ in 0..0 {
| - move occurs because `a` has type `&mut std::string::String`, which does not implement the `Copy` trait
8 | foo(a)
| ^ value moved here, in previous iteration of loop
error[E0499]: cannot borrow `b` as mutable more than once at a time
--> src/lib.rs:20:7
|
19 | g(&mut b); //works
| ------ first mutable borrow occurs here
20 | g(&mut b); //fails
| - ^^^^^^ second mutable borrow occurs here
| |
| first borrow later used by call
error: aborting due to 2 previous errors
Some errors have detailed explanations: E0382, E0499.
For more information about an error, try `rustc --explain E0382`.
error: Could not compile `playground`.
To learn more, run the command again with --verbose.
The text was updated successfully, but these errors were encountered:
This is a consequence of the fact that a closure is only given higher-ranked trait bound (HRTB) lifetimes if the presence of these lifetimes in its signature can be seen at the time the closure is type-checked.
Note that the full |a: &mut String| is not necessary, only the |a: &mut _|. Basically:
|a| foo(a) will create an fn(?A) -> ?B, where ?A and ?B are inference variables (types to be filled in later)
|a: &mut _| foo (a) will create an fn(&mut ?A) -> ?B, which is shorthand for for<'a> fn(&'a mut ?A) -> ?B. The for<'a> is the key missing piece.
On hearing this you might wonder why you've never had a similar problem with things like slice.iter_mut().map(|x| foo(x)), and that's because the compiler has a special case when type checking closures that appear as function arguments (it will infer types from Fn bounds). So this problem (and many other related problems with type inference) tends to rear its ugly head only in comparatively uncommon scenarios, such as when assigning a closure to a local variable with let.
(Playground)
Errors:
The text was updated successfully, but these errors were encountered: