Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extern crate owning_ref;
- use std::collections::HashMap;
- use std::collections::hash_map::Entry;
- use owning_ref::OwningRef;
- struct Foo {
- v: OwningRef<String, str>,
- // Other data not important for the question
- }
- fn main() {
- let mut l = Vec::<Foo>::new();
- let mut hash = HashMap::<&str, usize>::new();
- for i in (0..10).chain((0..10).rev()) {
- let s = format!("aaa {}", i);
- let s = OwningRef::new(s);
- let foo = Foo { v: s };
- let sref = foo.v.as_ref();
- let idx = match hash.entry(sref) { //a
- Entry::Occupied(ent) => {
- *ent.get()
- }
- Entry::Vacant(ent) => {
- l.push(foo); //b
- ent.insert(l.len() - 1);
- l.len() - 1
- }
- };
- // do something with idx
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement