Skip to content

src/flow_control/for.md into_iter() example invalid #1904

Open
@andree182

Description

@andree182

The code doesn't compile (not even online):

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() {
    |                      ++++++++

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions