Skip to content

Instantly share code, notes, and snippets.

@jroweboy
Created March 23, 2014 09:59
Show Gist options
  • Save jroweboy/9720969 to your computer and use it in GitHub Desktop.
Save jroweboy/9720969 to your computer and use it in GitHub Desktop.
tmp/recursive.rs:19:16: 19:38 error: borrowed value does not live long enough
tmp/recursive.rs:19 curr = curr.unwrap().children.find_or_insert(a.clone(), Some(Node::new(a.clone())));
^~~~~~~~~~~~~~~~~~~~~~
tmp/recursive.rs:15:11: 21:2 note: reference must be valid for the block at 15:10...
tmp/recursive.rs:15 fn main() {
tmp/recursive.rs:16 let v = ~[~"Hello", ~"Tree", ~"World"];
tmp/recursive.rs:17 let mut curr = &mut Some(Node::new(~"Testing"));
tmp/recursive.rs:18 for a in v.iter() {
tmp/recursive.rs:19 curr = curr.unwrap().children.find_or_insert(a.clone(), Some(Node::new(a.clone())));
tmp/recursive.rs:20 }
...
tmp/recursive.rs:19:9: 19:92 note: ...but borrowed value is only valid for the statement at 19:8
tmp/recursive.rs:19 curr = curr.unwrap().children.find_or_insert(a.clone(), Some(Node::new(a.clone())));
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tmp/recursive.rs:19:16: 19:20 error: cannot move out of dereference of `&mut`-pointer
tmp/recursive.rs:19 curr = curr.unwrap().children.find_or_insert(a.clone(), Some(Node::new(a.clone())));
^~~~
error: aborting due to 2 previous errors
extern crate collections;
use collections::hashmap::HashMap;
struct Node {
data : ~str,
children : ~HashMap<~str, Option<Node>>,
}
impl Node {
fn new(data: ~str) -> Node {
Node{ data: data.clone(), children: ~HashMap::new() }
}
}
fn main() {
let v = ~[~"Hello", ~"Tree", ~"World"];
let mut curr = &mut Some(Node::new(~"Testing"));
for a in v.iter() {
curr = curr.unwrap().children.find_or_insert(a.clone(), Some(Node::new(a.clone())));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment