Skip to content

Make "cannot move out of borrowed content" more beginner friendly #55153

Closed
@oli-obk

Description

@oli-obk

The following code emits "cannot move out of borrowed content" without any hint about the reason whatsoever. I think we should add a "help" pointing to the self argument of talk and stating something along the lines of "because of consuming argument here". If it's in the same crate, maybe even suggest to change it to a reference.

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 (file:///playground)
error[E0507]: cannot move out of borrowed content
  --> src/main.rs:14:5
   |
14 |     sheep.talk();
   |     ^^^^^ cannot move out of borrowed content

error: aborting due to previous error

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

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.NLL-diagnosticsWorking towards the "diagnostic parity" goalT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions