Created
February 7, 2014 07:53
-
-
Save pzol/8858770 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
: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 offoo.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(){