Skip to content

Instantly share code, notes, and snippets.

@rust-play rust-play/playground.rs Secret

Created Apr 2, 2020
Embed
What would you like to do?
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
You can’t perform that action at this time.