Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created April 2, 2020 16:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rust-play/c5b7e4c7e8a057e28c2559f64d7da6bd to your computer and use it in GitHub Desktop.
Save rust-play/c5b7e4c7e8a057e28c2559f64d7da6bd to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
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