Skip to content

Instantly share code, notes, and snippets.

@pzol
Created February 7, 2014 07:53
Show Gist options
  • Save pzol/8858770 to your computer and use it in GitHub Desktop.
Save pzol/8858770 to your computer and use it in GitHub Desktop.
struct Foo<'a> {
name: &'a str
}
impl<'a> Foo<'a> {
fn name(&'a mut self, s: &'a str) -> &'a mut Foo<'a> {
self.name = s;
self
}
}
#[test]
fn test_in_struct(){
let s = ~"foo";
let mut foo = Foo { name: s.as_slice() };
assert_eq!(foo.name, "foo");
foo.name("bar");
assert_eq!(foo.name, "bar");
}
@pzol
Copy link
Author

pzol commented Feb 7, 2014

:4:29: 4:39 error: cannot borrow foo.name because it is already borrowed as mutable
:4 let given_val = &($given);
^~~~~~~~~~
:1:1: 1:1 note: in expansion of assert_eq!
/Users/pzol/Dropbox/src/rust/one/src/gist/chaining_test.rs:21:3: 21:31 note: expansion site
/Users/pzol/Dropbox/src/rust/one/src/gist/chaining_test.rs:20:3: 20:6 note: previous borrow of foo.name as mutable occurs here; the mutable borrow prevents subsequent moves, borrows, or modification of foo.name until the borrow ends
/Users/pzol/Dropbox/src/rust/one/src/gist/chaining_test.rs:20 foo.name("bar");
^~~
/Users/pzol/Dropbox/src/rust/one/src/gist/chaining_test.rs:22:2: 22:2 note: previous borrow ends here
/Users/pzol/Dropbox/src/rust/one/src/gist/chaining_test.rs:14 fn test_in_struct(){

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