Skip to content

nll suggestion about moving out of a &T is to make it a &mut T #55154

Closed
@oli-obk

Description

@oli-obk
struct Sheep {
    message: String
}

impl Sheep {
    fn talk(self) {
        println!("{}", self.message);
    }
}

fn main()
{
    let sheep = &Sheep { message: "Määh".into() };
    sheep.talk();
}

(Playground)

Errors:

   Compiling playground v0.0.1 (/playground)
error[E0507]: cannot move out of borrowed content
  --> src/main.rs:14:5
   |
14 |     sheep.talk();
   |     ^^^^^ cannot move out of borrowed content

error[E0507]: cannot move out of `*sheep` which is behind a `&` reference
  --> src/main.rs:14:5
   |
13 |     let sheep = &Sheep { message: "Määh".into() };
   |                 --------------------------------- help: consider changing this to be a mutable reference: `&mut Sheep { message: "Määh".into() }`
14 |     sheep.talk();
   |     ^^^^^
   |     |
   |     cannot move out of `*sheep` which is behind a `&` reference
   |     `sheep` is a `&` reference, so the data it refers to cannot be moved

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0507`.
error: Could not compile `playground`.

To learn more, run the command again with --verbose.

Metadata

Metadata

Assignees

Labels

A-NLLArea: Non-lexical lifetimes (NLL)NLL-diagnosticsWorking towards the "diagnostic parity" goal

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions