-
-
Save rust-play/c5b7e4c7e8a057e28c2559f64d7da6bd to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
This file contains 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
use std::cell::RefCell; | |
use std::collections::HashMap; | |
struct Bar<'a> { | |
p: HashMap<String, RefCell<u8>>, | |
_map: HashMap<u8, Vec<&'a RefCell<u8>>>, | |
} | |
impl<'a> Bar<'a> { | |
fn new() -> Self { | |
Bar { | |
p: HashMap::new(), | |
_map: HashMap::new(), | |
} | |
} | |
fn foo(self: &mut Self) { | |
match self.p.get("a") { | |
Some(e) => { | |
// push an element in the vec contained in _map at key "8" | |
// here e is a ref on the refcell | |
let v: &mut Vec<&'a RefCell<u8>> = self._map.entry(8u8).or_default(); | |
v.push(e); | |
// from what I understand, e has the lifetime of p and it does not match with 'a | |
// but since lifetime of p is equal to lifetime of self which is equal to 'a | |
// I don't understand why it bugs here ... | |
} | |
None => {} | |
}; | |
} | |
} | |
fn main() { | |
println!("Hello, world!"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment