4 ms·
Searching[1] a data-structure (like a hashmap) will return a borrowed reference to that data, which "freezes" the hashmap while the borrow exists. And, due to l
by dbaupp 12y ago
Searching[1] a data-structure (like a hashmap) will return a borrowed reference to that data, which "freezes" the hashmap while the borrow exists. And, due to lexical borrowing, it is frozen even if there was nothing (that is, if the return value was `None` which contains no references linked to the original map).
The following is currently illegal because the `insert` is trying to modify the borrowed map.
match some_map.find(&a_key) {
Some(x) => println!("reference to value {}", x),
None => {
println!("not found, inserting instead");
some_map.insert(a_key, some_value);
}
}
#6393 is the relevant issue.
[1]: http://doc.rust-lang.org/nightly/std/collections/struct.HashMap.html#method.find http://doc.rust-lang.org/nightly/std/collections/struct.Hash...
[6393]: https://github.com/rust-lang/rust/issues/6393 https://github.com/rust-lang/rust/issues/6393
- Ygg2 12y agoHow exactly does one decide if scope is lexical or non-lexical? Or more precisely how do you tell compiler?
- steveklabnik 12y agoThere is a Drop trait that has one method, drop(). It's like a destructor. It's called for you, but you can also call it yourself. fn foo(x: &something) { // stuff x.drop(); // more stuff, } // if we didn't drop, x gets drop()-ped here If we had non-lexical borrows, after we drop x, we could have it be un-borrowed. currently, x is still considered borrowed until the end of foo(). This is sort of a contrived example, but if you check out the ticket it has better ones. There's also good examples here: http://www.reddit.com/r/rust/comments/2hy06n/a_fresh_look_at_rust_armin_ronachers_thoughts_and/ckxdh5w http://www.reddit.com/r/rust/comments/2hy06n/a_fresh_look_at...