Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #![feature(nll)]
- use std::collections::HashMap;
- struct Node {
- children: HashMap<String, Option<Box<Node>>>,
- }
- fn main() {
- let mut node = Node::new("xyz");
- // cur_node was in another function and pointed to root of tree.
- let mut cur_node = &mut node;
- // I wanted to traverse the nodes.
- // So, The if block in this gets a immutable reference to node
- // The code in else tries to get a mutable reference and update the node.
- // but it doesn't works! because cur_node.children.get() has a mutable reference and there can only be one mutable reference.
- if let Some(node) = cur_node.children.get_mut("abc") {
- cur_node = node.as_mut().unwrap();
- } else {
- // Create folder
- let n = Box::new(Node::new("abc"));
- // This does not works. It returns as_mut not found on &Node
- cur_node.children.insert("abc".into(), Some(n));
- }
- }
- impl Node {
- fn new(name: &str) -> Node {
- let mut h = HashMap::new();
- h.insert(name.to_owned(), None);
- Node { children: h }
- }
- }
Add Comment
Please, Sign In to add comment