Remove found entry from HashMap without cloning the key - rust

I am finding a certain entry in a HashMap (in this case it's the one that's "least used"). I now want to remove that entry from the map. Since the key was obtained from the HashMap itself (and thus is a reference), how can I now use it to remove the entry without cloning the key.
Here is a small, runnable example:
use std::collections::HashMap;
fn main() {
let mut usage = HashMap::<String, usize>::new();
usage.insert("entry one".to_owned(), 5);
usage.insert("entry two".to_owned(), 1);
let mut least_used: Option<(&String, &usize)> = None;
for curr in usage.iter() {
if let Some(prev) = least_used {
if curr.1 < prev.1 {
least_used = Some(curr);
}
} else {
least_used = Some(curr);
}
}
println!("{:?}", least_used);
usage.remove(least_used.unwrap().0);
}
Rust playground
The error I'm getting is:
cannot borrow `usage` as mutable because it is also borrowed as immutable

Related

How do I tackle lifetimes in Rust?

I am having issues with the concept of lifetimes in rust. I am trying to use the crate bgpkit_parser to read in a bz2 file via url link and then create a radix trie.
One field extracted from the file is the AS Path which I have named path in my code within the build_routetable function. I am having trouble as to why rust does not like let origin = clean_path.last() which takes the last element in the vector.
fn as_parser(element: &BgpElem) -> Vec<u32> {
let x = &element.as_path.as_ref().unwrap().segments[0];
let mut as_vec = &Vec::new();
let mut as_path: Vec<u32> = Vec::new();
if let AsPathSegment::AsSequence(value) = x {
as_vec = value;
}
for i in as_vec {
as_path.push(i.asn);
}
return as_path;
}
fn prefix_parser(element: &BgpElem) -> String {
let subnet_id = element.prefix.prefix.ip().to_string().to_owned();
let prefix_id = element.prefix.prefix.prefix().to_string().to_owned();
let prefix = format!("{}/{}", subnet_id, prefix_id);//.as_str();
return prefix;
}
fn get_aspath(raw_aspath: Vec<u32>) -> Vec<u32> {
let mut as_path = Vec::new();
for i in raw_aspath {
if i < 64511 {
if as_path.contains(&i) {
continue;
}
else {
as_path.push(i);
}
}
else if 65535 < i && i < 4000000000 {
if as_path.contains(&i) {
continue;
}
else {
as_path.push(i);
}
}
}
return as_path;
}
fn build_routetable(mut trie4: Trie<String, Option<&u32>>, mut trie6: Trie<String, Option<&u32>>) {
let url: &str = "http://archive.routeviews.org/route-views.chile/\
bgpdata/2022.06/RIBS/rib.20220601.0000.bz2";
let parser = BgpkitParser::new(url).unwrap();
let mut count = 0;
for elem in parser {
if elem.elem_type == bgpkit_parser::ElemType::ANNOUNCE {
let record_timestamp = &elem.timestamp;
let record_type = "A";
let peer = &elem.peer_ip;
let prefix = prefix_parser(&elem);
let path = as_parser(&elem);
let clean_path = get_aspath(path);
// Issue is on the below line
// `clean_path` does not live long enough
// borrowed value does not live long
// enough rustc E0597
// main.rs(103, 9): `clean_path` dropped
// here while still borrowed
// main.rs(77, 91): let's call the
// lifetime of this reference `'1`
// main.rs(92, 17): argument requires
// that `clean_path` is borrowed for `'1`
let origin = clean_path.last(); //issue line
if prefix.contains(":") {
trie6.insert(prefix, origin);
}
else {
trie4.insert(prefix, origin);
}
count+=1;
if count >= 10000 {
println!("{:?} | {:?} | {:?} | {:?} | {:?}",
record_type, record_timestamp, peer, prefix, path);
count=0
}
};
}
println!("Trie4 size: {:?} prefixes", trie4.len());
println!("Trie6 size: {:?} prefixes", trie6.len());
}
Short answer: you're "inserting" a reference. But what's being referenced doesn't outlive what it's being inserted into.
Longer: The hint is your trie4 argument, the signature of which is this:
mut trie4: Trie<String, Option<&u32>>
So that lives beyond the length of the loop where things are declared. This is all in the loop:
let origin = clean_path.last(); //issue line
if prefix.contains(":") {
trie6.insert(prefix, origin);
}
While origin is a Vec<u32> and that's fine, the insert method is no doubt taking a String and either an Option<&u32> or a &u32. Obviously a key/value pair. But here's your problem: the value has to live as long as the collection, but your value is the last element contained in the Vec<u32>, which goes away! So you can't put something into it that will not live as long as the "container" object! Rust has just saved you from dangling references (just like it's supposed to).
Basically, your containers should be Trie<String, Option<u32>> without the reference, and then this'll all just work fine. Your problem is that the elements are references, and not just contained regular values, and given the size of what you're containing, it's actually smaller to contain a u32 than a reference (pointer size (though actually, it'll likely be the same either way, because alignment issues)).
Also of note: trie4 and trie6 will both be gone at the end of this function call, because they were moved into this function (not references or mutable references). I hope that's what you want.

Process value in Hashmap based on another value in same Hashmap

I have a HashMap acting as a lookup table in my code, mapping IDs <-> Data.
I need to lookup some data (let's call it Data A) based on my ID, then read the contents. Based on entries in the content, I would then need to lookup another value in the same lookup table, read those data, and do some calculations, updating my original data A.
Here is a minimal working example:
playground
use std::collections::HashMap;
struct MyData {
id: i32,
result: i32,
complex_data: Vec<i32>
}
impl MyData {
fn new(id: i32) -> Self {
MyData {
id,
result: 0,
complex_data: Vec::new()
}
}
}
fn main() {
let mut lookup_table = HashMap::new();
// init data
lookup_table.insert(1, MyData::new(1));
lookup_table.insert(2, MyData::new(2));
lookup_table.insert(3, MyData::new(3));
lookup_table.insert(4, MyData::new(4));
// process data based on an ID. In this example, hard coded as "1"
if let Some(data) = lookup_table.get_mut(&1) {
// process each entry
for c in data.complex_data.iter() {
// lookup some more values based the entry
if let Some(lookup_data) = lookup_table.get(c) {
//^^^^^^^^^^^^^^^^^^^ - cannot borrow `lookup_table` as immutable
// do some calculation and store result
data.result = lookup_data.result + 42; // random calculation as an example
}
}
}
println!("Hello, world!");
}
The error occurs because it seems I'm borrowing lookup_table twice. From what I understand, the compiler is worried that my second lookup also looks up the ID = 1, which will mean I have an mutable reference of DataID = 1, and an immutable reference of DataID = 1 at the same time.
I am fine with this, however, since my second read is immutable, and also this whole thing is single-threaded, so I'm not worried about any race conditions.
How can I restructure my code to make the Rust compiler happy whilst achieving my functionality?
I think you can work around the issue by doing all the reads with an immutable borrow at the first part of the if statement, saving the calculation results into a temporary vector, and doing all the writes with a mutable borrow at the second part. See the code below.
// process data based on an ID. In this example, hard coded as "1"
if let Some(data) = lookup_table.get(&1) {
let mut results = Vec::new();
// process each entry
for c in data.complex_data.iter() {
// lookup some more values based the entry
if let Some(lookup_data) = lookup_table.get(c) {
// do some calculation and store result
results.push(lookup_data.result);
}
}
let data = lookup_table.get_mut(&1).unwrap();
for v in results {
data.result = v + 42;
}
}
The latter assignment to data shadows the previous one and ends the lifetime of the immutable borrow.
Playground
You can use interior mutability pattern on result field. This gives you the possibility to make an immutable borrow &MyData in the outer loop, and mutate its result field in the inner loop. The borrow checker doesn't complain because all checks are done at runtime.
And at runtime, you never have several mutable ref at the same time.
use std::{cell::RefCell, collections::HashMap};
struct MyData {
id: i32,
result: RefCell<i32>,
complex_data: Vec<i32>,
}
impl MyData {
fn new(id: i32) -> Self {
MyData {
id,
result: RefCell::new(0),
complex_data: vec![1, 2, 3, 4],
}
}
fn set_result(&self, result: i32) {
*self.result.borrow_mut() = result;
}
fn get_result(&self) -> i32 {
self.result.take()
}
}
fn main() {
let mut lookup_table = HashMap::new();
// init data
lookup_table.insert(1, MyData::new(1));
lookup_table.insert(2, MyData::new(2));
lookup_table.insert(3, MyData::new(3));
lookup_table.insert(4, MyData::new(4));
// process data based on an ID. In this example, hard coded as "1"
if let Some(data) = lookup_table.get(&1) {
// process each entry
for c in data.complex_data.iter() {
// lookup some more values based the entry
if let Some(lookup_data) = lookup_table.get(c) {
//^^^^^^^^^^^^^^^^^^^ - cannot borrow `lookup_table` as immutable
// do some calculation and store result
data.set_result(lookup_data.get_result() + 42);
// random calculation as an example
}
}
}
}
If you don't want to pay the runtime cost, you can use the interior mutability pattern with Cell instead of RefCell.
Interior mutability is one option, as presented in a previous answer. Collecting the values is another option, as presented in another previous answer. Depending on the nature of your calculation, you might not need any allocation at all, but just store intermediate results in a global variable, and assign it at the end.
For example, this compiles:
fn main() {
let mut lookup_table = HashMap::from([
(1, MyData::new(1)),
(2, MyData::new(2)),
(3, MyData::new(3)),
(4, MyData::new(4)),
]);
let data_key = 1;
let mut to_store = None;
if let Some(data) = lookup_table.get(&data_key) {
let mut result = data.result;
for subkey in &data.complex_data {
if let Some(sub_data) = lookup_table.get(subkey) {
result += sub_data.result + 42;
}
}
to_store = Some(result);
}
if let Some(to_store) = to_store {
lookup_table.get_mut(&data_key).unwrap().result = to_store;
}
println!("Hello, world!");
}
Playground

Equivalent of split_at_mut for HashMap?

Consider the following code (minimum example):
use std::collections::HashMap;
fn main() {
let mut map: HashMap<usize, i128> = HashMap::new();
map.insert(1, -5);
map.insert(2, 6);
map.insert(3, 7);
for i in map.keys() {
if *i == 3 {
continue;
}
*map.get_mut(&3).unwrap() += map[i];
}
}
The borrow checker will complain that:
cannot borrow `map` as mutable because it is also borrowed as immutable
However, in this case I can be sure that the mutation I am doing is not interfering with the immutable references. For Vec, I would use split_at_mut here - is there an equivalent for that for HashMap in Rust?
Edit:
As a comment pointed out, let me be more specific about the problem I am trying to solve. I want to implement "merging" vertices in a graph into one vertex. For this I created a HashMap:
pub mergedVerticesList: HashMap<usize, HashSet<usize>>
which should map from a vertex to all the vertices that have been merged into that vertex. Since this can be done recursively, when undoing a merge between u and merge_onto I want to remove all vertices that have been merged into merge_onto because of u from merge_onto's HashSet. Thus, the code looks like this:
for i in self.mergedVerticesList[v].iter() {
self.mergedVerticesList.get_mut(&merge_onto).unwrap().remove(i);
}
No, this is not possible as far as the HashMap is concerned. You could have cells as values to regain mutable access but I'd strongly advise against that. Can't you move the change out of the loop (it also saves from a repeated lookup per iteration). Something to the tune of
use std::collections::HashMap;
fn main() {
let mut map: HashMap<usize, i128> = HashMap::new();
map.insert(1, -5);
map.insert(2, 6);
map.insert(3, 7);
let s = map.iter().filter_map(|(k, v)| if *k != 3 { Some(v) } else { None }).sum::<i128>();
*map.get_mut(&3).unwrap() += s;
println!("{:#?}", &map)
}

Why does the Rust compiler complain that I use a moved value when I've replaced it with a new value?

I am working on two singly linked lists, named longer and shorter. The length of the longer one is guaranteed to be no less than the shorter one.
I pair the lists element-wise and do something to each pair. If the longer list has more unpaired elements, process the rest of them:
struct List {
next: Option<Box<List>>,
}
fn drain_lists(mut shorter: Option<Box<List>>, mut longer: Option<Box<List>>) {
// Pair the elements in the two lists.
while let (Some(node1), Some(node2)) = (shorter, longer) {
// Actual work elided.
shorter = node1.next;
longer = node2.next;
}
// Process the rest in the longer list.
while let Some(node) = longer {
// Actual work elided.
longer = node.next;
}
}
However, the compiler complains on the second while loop that
error[E0382]: use of moved value
--> src/lib.rs:13:20
|
5 | fn drain_lists(mut shorter: Option<Box<List>>, mut longer: Option<Box<List>>) {
| ---------- move occurs because `longer` has type `std::option::Option<std::boxed::Box<List>>`, which does not implement the `Copy` trait
6 | // Pair the elements in the two lists.
7 | while let (Some(node1), Some(node2)) = (shorter, longer) {
| ------ value moved here
...
13 | while let Some(node) = longer {
| ^^^^ value used here after move
However, I do set a new value for shorter and longer at the end of the loop, so that I will never use a moved value of them.
How should I cater to the compiler?
I think that the problem is caused by the tuple temporary in the first loop. Creating a tuple moves its components into the new tuple, and that happens even when the subsequent pattern matching fails.
First, let me write a simpler version of your code. This compiles fine:
struct Foo(i32);
fn main() {
let mut longer = Foo(0);
while let Foo(x) = longer {
longer = Foo(x + 1);
}
println!("{:?}", longer.0);
}
But if I add a temporary to the while let then I'll trigger a compiler error similar to yours:
fn fwd<T>(t: T) -> T { t }
struct Foo(i32);
fn main() {
let mut longer = Foo(0);
while let Foo(x) = fwd(longer) {
longer = Foo(x + 1);
}
println!("{:?}", longer.0);
// Error: ^ borrow of moved value: `longer`
}
The solution is to add a local variable with the value to be destructured, instead of relying on a temporary. In your code:
struct List {
next: Option<Box<List>>
}
fn drain_lists(shorter: Option<Box<List>>,
longer: Option<Box<List>>) {
// Pair the elements in the two lists.
let mut twolists = (shorter, longer);
while let (Some(node1), Some(node2)) = twolists {
// Actual work elided.
twolists = (node1.next, node2.next);
}
// Process the rest in the longer list.
let (_, mut longer) = twolists;
while let Some(node) = longer {
// Actual work elided.
longer = node.next;
}
}
Other than getting rid of the tuple (shown by others), you can capture a mutable reference to the nodes:
while let (&mut Some(ref mut node1), &mut Some(ref mut node2)) = (&mut shorter, &mut longer) {
shorter = node1.next.take();
longer = node2.next.take();
}
The use of take() enables this to work: shorter = node1.next would complain of moving a field out of a reference, which is not allowed (it would leave the node in an undefined state). But takeing it is ok because it leaves None in the next field.
Looks like the destructuring on line 7 moves the value even when the block afterwards is not evaluated. (Edit: as #Sven Marnach pointed out in the comments, a temporary tuple gets created here which causes the move)
I've uglyfied your code to prove that point :)
struct List {
next: Option<Box<List>>
}
fn drain_lists(mut shorter: Option<Box<List>>,
mut longer: Option<Box<List>>) {
// Pair the elements in the two lists.
match(shorter, longer) {
(Some(node1), Some(node2)) => {
shorter = node1.next;
longer = node2.next;
},
(_, _) => return // without this you get the error
}
// Process the rest in the longer list.
while let Some(node) = longer {
// Actual work elided.
longer = node.next;
}
}
When I added the return for the default case, the code compiled.
One solution is to avoid the tuple and consequently the move of longer into the tuple.
fn actual_work(node1: &Box<List>, node2: &Box<List>) {
// Actual work elided
}
fn drain_lists(mut shorter: Option<Box<List>>, mut longer: Option<Box<List>>) {
while let Some(node1) = shorter {
if let Some(node2) = longer.as_ref() {
actual_work(&node1, node2);
}
shorter = node1.next;
longer = longer.map_or(None, move |l| {
l.next
});
}
// Process the rest in the longer list.
while let Some(node) = longer {
// Actual work elided.
longer = node.next;
}
}

Bind a reference to a struct property to a variable inside a function returning a mutable reference [duplicate]

I am learning Rust and I don't quite get why this is not working.
#[derive(Debug)]
struct Node {
value: String,
}
#[derive(Debug)]
pub struct Graph {
nodes: Vec<Box<Node>>,
}
fn mk_node(value: String) -> Node {
Node { value }
}
pub fn mk_graph() -> Graph {
Graph { nodes: vec![] }
}
impl Graph {
fn add_node(&mut self, value: String) {
if let None = self.nodes.iter().position(|node| node.value == value) {
let node = Box::new(mk_node(value));
self.nodes.push(node);
};
}
fn get_node_by_value(&self, value: &str) -> Option<&Node> {
match self.nodes.iter().position(|node| node.value == *value) {
None => None,
Some(idx) => self.nodes.get(idx).map(|n| &**n),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn some_test() {
let mut graph = mk_graph();
graph.add_node("source".to_string());
graph.add_node("destination".to_string());
let source = graph.get_node_by_value("source").unwrap();
let dest = graph.get_node_by_value("destination").unwrap();
graph.add_node("destination".to_string());
}
}
(playground)
This has the error
error[E0502]: cannot borrow `graph` as mutable because it is also borrowed as immutable
--> src/main.rs:50:9
|
47 | let source = graph.get_node_by_value("source").unwrap();
| ----- immutable borrow occurs here
...
50 | graph.add_node("destination".to_string());
| ^^^^^ mutable borrow occurs here
51 | }
| - immutable borrow ends here
This example from Programming Rust is quite similar to what I have but it works:
pub struct Queue {
older: Vec<char>, // older elements, eldest last.
younger: Vec<char>, // younger elements, youngest last.
}
impl Queue {
/// Push a character onto the back of a queue.
pub fn push(&mut self, c: char) {
self.younger.push(c);
}
/// Pop a character off the front of a queue. Return `Some(c)` if there /// was a character to pop, or `None` if the queue was empty.
pub fn pop(&mut self) -> Option<char> {
if self.older.is_empty() {
if self.younger.is_empty() {
return None;
}
// Bring the elements in younger over to older, and put them in // the promised order.
use std::mem::swap;
swap(&mut self.older, &mut self.younger);
self.older.reverse();
}
// Now older is guaranteed to have something. Vec's pop method // already returns an Option, so we're set.
self.older.pop()
}
pub fn split(self) -> (Vec<char>, Vec<char>) {
(self.older, self.younger)
}
}
pub fn main() {
let mut q = Queue {
older: Vec::new(),
younger: Vec::new(),
};
q.push('P');
q.push('D');
assert_eq!(q.pop(), Some('P'));
q.push('X');
let (older, younger) = q.split(); // q is now uninitialized.
assert_eq!(older, vec!['D']);
assert_eq!(younger, vec!['X']);
}
A MRE of your problem can be reduced to this:
// This applies to the version of Rust this question
// was asked about; see below for updated examples.
fn main() {
let mut items = vec![1];
let item = items.last();
items.push(2);
}
error[E0502]: cannot borrow `items` as mutable because it is also borrowed as immutable
--> src/main.rs:4:5
|
3 | let item = items.last();
| ----- immutable borrow occurs here
4 | items.push(2);
| ^^^^^ mutable borrow occurs here
5 | }
| - immutable borrow ends here
You are encountering the exact problem that Rust was designed to prevent. You have a reference pointing into the vector and are attempting to insert into the vector. Doing so might require that the memory of the vector be reallocated, invalidating any existing references. If that happened and you used the value in item, you'd be accessing uninitialized memory, potentially causing a crash.
In this particular case, you aren't actually using item (or source, in the original) so you could just... not call that line. I assume you did that for some reason, so you could wrap the references in a block so that they go away before you try to mutate the value again:
fn main() {
let mut items = vec![1];
{
let item = items.last();
}
items.push(2);
}
This trick is no longer needed in modern Rust because non-lexical lifetimes have been implemented, but the underlying restriction still remains — you cannot have a mutable reference while there are other references to the same thing. This is one of the rules of references covered in The Rust Programming Language. A modified example that still does not work with NLL:
let mut items = vec![1];
let item = items.last();
items.push(2);
println!("{:?}", item);
In other cases, you can copy or clone the value in the vector. The item will no longer be a reference and you can modify the vector as you see fit:
fn main() {
let mut items = vec![1];
let item = items.last().cloned();
items.push(2);
}
If your type isn't cloneable, you can transform it into a reference-counted value (such as Rc or Arc) which can then be cloned. You may or may not also need to use interior mutability:
struct NonClone;
use std::rc::Rc;
fn main() {
let mut items = vec![Rc::new(NonClone)];
let item = items.last().cloned();
items.push(Rc::new(NonClone));
}
this example from Programming Rust is quite similar
No, it's not, seeing as how it doesn't use references at all.
See also
Cannot borrow `*x` as mutable because it is also borrowed as immutable
Pushing something into a vector depending on its last element
Why doesn't the lifetime of a mutable borrow end when the function call is complete?
How should I restructure my graph code to avoid an "Cannot borrow variable as mutable more than once at a time" error?
Why do I get the error "cannot borrow x as mutable more than once"?
Why does Rust want to borrow a variable as mutable more than once at a time?
Try to put your immutable borrow inside a block {...}.
This ends the borrow after the block.
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn some_test() {
let mut graph = mk_graph();
graph.add_node("source".to_string());
graph.add_node("destination".to_string());
{
let source = graph.get_node_by_value("source").unwrap();
let dest = graph.get_node_by_value("destination").unwrap();
}
graph.add_node("destination".to_string());
}
}
So for anyone else banging their head against this problem and wanting a quick way out - use clones instead of references. Eg I'm iterating this list of cells and want to change an attribute so I first copy the list:
let created = self.cells
.into_iter()
.map(|c| {
BoardCell {
x: c.x,
y: c.y,
owner: c.owner,
adjacency: c.adjacency.clone(),
}
})
.collect::<Vec<BoardCell>>();
And then modify the values in the original by looping the copy:
for c in created {
self.cells[(c.x + c.y * self.size) as usize].adjacency[dir] = count;
}
Using Vec<&BoardCell> would just yield this error. Not sure how Rusty this is but hey, it works.

Resources