Skip to content

Instantly share code, notes, and snippets.

@dinfuehr
Created Jan 26, 2015
Embed
What would you like to do?
Rust: cannot move out of borrowed content
struct Foo {
value: String
}
impl Foo {
fn test(&mut self) -> String {
let old = self.value;
self.value = "new".to_string();
old
}
}
fn main() {
let mut foo = Foo { value: "old".to_string() };
assert_eq!("old", foo.test());
println!("foo = {}", foo.value);
}
@dinfuehr
Copy link
Author

dinfuehr commented Jan 26, 2015

Error is thrown at line 7. How can I replace the value in the struct, but also retain the old value?

@dinfuehr
Copy link
Author

dinfuehr commented Jan 26, 2015

Can be solved with mem::replace(&mut self.value, "new".to_string()). see documentation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment