fn main() {
let a = String::from("foo");
let b = create_b(&a);
let _moved_a = a;
b.test();
}
fn create_b(_unused: &str) -> B {
B { b: "foobar" }
}
struct B<'a> {
b: &'a str,
}
impl<'a> B<'a> {
fn test(&self) -> Option<&str> {
None
}
}
Errors:
Compiling playground v0.0.1 (/playground)
error[E0505]: cannot move out of `a` because it is borrowed
--> src/main.rs:4:20
|
3 | let b = create_b(&a);
| -- borrow of `a` occurs here
4 | let _moved_a = a;
| ^ move out of `a` occurs here
5 | b.test();
| - borrow later used here
error: aborting due to previous error
For more information about this error, try `rustc --explain E0505`.
error: could not compile `playground`
To learn more, run the command again with --verbose.
I don't understand the error here.
Can anyone help me understand how "a" and "b" are related?
I found out about the Polonius borrow checker and tried that as well but it has the same error.
This is a stripped down example. It happened when using App
from clap
in our code.