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
error[E0382]: borrow of moved value: `names`
--> src/main.rs:11:29
|
2 | let names = vec!["Bob", "Frank", "Ferris"];
| ----- move occurs because `names` has type `Vec<&str>`, which does not implement the `Copy` trait
3 |
4 | for name in names.into_iter() {
| ----------- `names` moved due to this method call
...
11 | println!("names: {:?}", names);
| ^^^^^ value borrowed here after move
|
note: `into_iter` takes ownership of the receiver `self`, which moves `names`
--> /playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/iter/traits/collect.rs:346:18
|
346 | fn into_iter(self) -> Self::IntoIter;
| ^^^^
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
help: you can `clone` the value and consume it, but this might not be your desired behavior
|
4 | for name in names.clone().into_iter() {
| ++++++++
The text was updated successfully, but these errors were encountered:
fnmain(){let names = vec!["Bob","Frank","Ferris"];for name in names.into_iter(){match name {"Ferris" => println!("There is a rustacean among us!"),
_ => println!("Hello {}", name),}}println!("names: {:?}", names);// FIXME ^ Comment out this line}
There is a FIXME line for learning purpose, so it's normal it's not working. This example show the list is being consumed by into_iter(), so it's not printable
I'd argue it's way too soon to introduce borrow checker issues here. It's confusing enough that "some 'traits'" are involved...
Inverse approach would IMO be better. Like having the final println! commented out, with a comment "// NOTE: This is invalid, because the 'names' is not owned anymore", or somesuch.
The code doesn't compile (not even online):
The text was updated successfully, but these errors were encountered: