Rust assignment and use borrow at the same time - rust

It confuses me that the error at line A has anything to do with line C, as they basically appear in different branches?
pub struct ListNode {
pub val: i32,
pub next: Option<Box<ListNode>>
}
pub fn foo(mut head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
let mut p1 = head.as_mut()?;
if let Some(p2) = p1.next.as_mut() {
if true {
p1.next = None; // A: Error here if C is not commented out
} else {
// p1.next = None; // B
p1 = p2; // C: No error on this line
}
}
head
To make things worse, the error has code E0506, suggesting an attempt is made to assign a borrowed value (p1.next). How come I cannot assign this borrowed value (which I believe is mutably borrowed) now but I can do that simply by deleting line C?
error[E0506]: cannot assign to `p1.next` because it is borrowed
--> src/remove_dup.rs:11:13
|
9 | if let Some(p2) = p1.next.as_mut() {
| ------- borrow of `p1.next` occurs here
10 | if true {
11 | p1.next = None; // A: Error happnens here if C is not commented out
| ^^^^^^^
| |
| assignment to borrowed `p1.next` occurs here
| borrow later used here

Related

Cryptic message when dealing with Rust Option type

I have a function like the following:
pub fn process_options<T>(in_refs:& mut Vec<T>,ivals:Vec<Option<T>>) -> i32 {
let mut nones = 0;
for int in 0..ivals.len() {
if let None = ivals[int] {
nones += 1;
} else {
in_refs.push(ivals[int].unwrap());
}
}
return nones;
}
What this function does is that it:
It pushes all the some(T) from one vector to another as T. And it also records the number of none values in the first vector. As written above the code fails to compile and gives me:
error[E0507]: cannot move out of index of `Vec<Option<T>>`
--> src/functions.rs:40:26
|
40 | in_refs.push(ivals[int].unwrap());
| ^^^^^^^^^^ -------- value moved due to this method call
| |
| help: consider calling `.as_ref()` or `.as_mut()` to borrow the type's contents
| move occurs because value has type `Option<T>`, which does not implement the `Copy` trait
|
note: this function takes ownership of the receiver `self`, which moves value
--> /Users/yunfeichen/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/src/rust/library/core/src/option.rs:772:25
|
772 | pub const fn unwrap(self) -> T {
Which I frankly do not understand can you explain this to me?
Edit:
This question has been solved:
It turns out I just need to use a different style of for loop, Not sure why this for loop works but it does:
pub fn process_options<T>(in_refs:& mut Vec<T>,ivals:Vec<Option<T>>) -> i32 {
let mut nones = 0;
for element in ivals{
if let None = element {
nones += 1;
} else {
in_refs.push(element.unwrap());
}
}
return nones;
}

How do I change the structure in the thread?

Please help me, I'm completely confused.
How do I make this code work?
I need to change the structure members in the thread...
#[derive(Debug)]
struct S {
str: String,
b: bool,
i: u128,
}
fn main() {
let mut vec_s = vec![];
vec_s.push(S {
str: "a".to_string(),
b: false,
i: 0,
});
let mut threads = vec![];
for s in vec_s {
{
let mut _s = &s;
threads.push(std::thread::spawn(move || {
_s.b = true;
_s.str = "b".to_string();
_s.i = 1;
}));
}
}
for thread in threads {
let _ = thread.join();
}
dbg!(&vec_s);
}
The compiler outputs a lot of errors:
error[E0594]: cannot assign to `_s.b`, which is behind a `&` reference
--> src/main.rs:23:17
|
23 | _s.b = true;
| ^^^^^^^^^^^ cannot assign
error[E0594]: cannot assign to `_s.str`, which is behind a `&` reference
--> src/main.rs:24:17
|
24 | _s.str = "b".to_string();
| ^^^^^^ cannot assign
error[E0594]: cannot assign to `_s.i`, which is behind a `&` reference
--> src/main.rs:25:17
|
25 | _s.i = 1;
| ^^^^^^^^ cannot assign
error[E0597]: `s` does not live long enough
--> src/main.rs:21:26
|
21 | let mut _s = &s;
| ^^ borrowed value does not live long enough
22 | threads.push(std::thread::spawn(move || {
| __________________________-
23 | | _s.b = true;
24 | | _s.str = "b".to_string();
25 | | _s.i = 1;
26 | | }));
| |______________- argument requires that `s` is borrowed for `'static`
27 | }
28 | }
| - `s` dropped here while still borrowed
error[E0382]: borrow of moved value: `vec_s`
--> src/main.rs:34:10
|
9 | let mut vec_s = vec![];
| --------- move occurs because `vec_s` has type `Vec<S>`, which does not implement the `Copy` trait
...
19 | for s in vec_s {
| ----- `vec_s` moved due to this implicit call to `.into_iter()`
...
34 | dbg!(&vec_s);
| ^^^^^^ value borrowed here after move
|
note: this function takes ownership of the receiver `self`, which moves `vec_s`
--> /home/martin/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/iter/traits/collect.rs:261:18
|
261 | fn into_iter(self) -> Self::IntoIter;
| ^^^^
help: consider iterating over a slice of the `Vec<S>`'s content to avoid moving into the `for` loop
|
19 | for s in &vec_s {
| +
You are attempting to do multithreading. Everything accessed by multiple threads has to be thread-safe. Further, with Rust being very strict (zero undefined behaviour tolerance), the compiler has to understand that your code is thread-safe.
The biggest problem here is that Rust's borrow checker doesn't understand that your threads get joined at some point. Therefore it doesn't allow you to create references to your S objects, because it cannot prove how long those references exist. Rust must be able to prove that they are destroyed before your S object is dropped; that's part of Rust's borrow checker safety guarantees.
For your specific usecase, Rust introduced thread scopes. With them, the compiler can finally understand thread lifetimes.
With some minor ownership issues fixed (use &mut vec_s for your loop, otherwise the vec_s object gets consumed by the loop), this now works:
#[derive(Debug)]
struct S {
s: String,
b: bool,
i: u128,
}
fn main() {
let mut vec_s = vec![];
vec_s.push(S {
s: "a".to_string(),
b: false,
i: 0,
});
std::thread::scope(|scope| {
let mut threads = vec![];
for s in &mut vec_s {
threads.push(scope.spawn(move || {
s.b = true;
s.s = "b".to_string();
s.i = 1;
}));
}
for thread in threads {
let _ = thread.join();
}
});
dbg!(&vec_s);
}
[src/main.rs:31] &vec_s = [
S {
s: "b",
b: true,
i: 1,
},
]
Another optimization:
If you don't actually use the JoinHandles for error propagation or similar, you don't need them at all in this example. The scope already automatically joins all threads it spawned at the end of the scope:
#[derive(Debug)]
struct S {
s: String,
b: bool,
i: u128,
}
fn main() {
let mut vec_s = vec![];
vec_s.push(S {
s: "a".to_string(),
b: false,
i: 0,
});
std::thread::scope(|scope| {
for s in &mut vec_s {
scope.spawn(move || {
s.b = true;
s.s = "b".to_string();
s.i = 1;
});
}
});
dbg!(&vec_s);
}
[src/main.rs:27] &vec_s = [
S {
s: "b",
b: true,
i: 1,
},
]

How to combine mutable and immutable borrowing inside one method? [duplicate]

This question already has answers here:
How to get mutable references to two array elements at the same time?
(8 answers)
Closed last year.
This is the code:
struct Bar {
x : i8
}
struct Foo {
items : [Bar; 2]
}
impl Foo {
pub fn update(&mut self, i : i8) {
let item = &mut self.items[0];
if self.items[1].x > 0 {
item.x = i
}
}
}
It doesn't compile:
error[E0503]: cannot use `self.items[_].x` because it was mutably borrowed
--> src/lib.rs:10:12
|
9 | let item = &mut self.items[0];
| ------------------ borrow of `self.items[_]` occurs here
10 | if self.items[1].x > 0 {
| ^^^^^^^^^^^^^^^ use of borrowed `self.items[_]`
11 | item.x = i
| ---------- borrow later used here
I believe I understand why, but what is a workaround? I do need to both modify and read the same piece of data inside the same method.
Not sure what the point in what you are trying to do is, but it can be done like this:
pub fn update(&mut self, i : i8) {
if self.items[1].x > 0 {
self.items[0].x = i
}
}

Rust borrow checker multiple mutable reference in modifying element in linked list in a loop

I'm trying to build some sort of linked list, each node has a keyword and vectors of the same node. In that for loop, i will traverse the linked list to get the vector, either already exist if keyword is the same, or create a new vector. I find it impossible to *not * violate the double mut reference error since I'll need to do iter_mut(), push() on the same path. Anyone has general idea on how to approach this?
pub struct Knowledge {
pub title: String,
}
pub struct Entry {
pub keyword: String,
pub next: Vec<Box<KnowledgeEntry>>
}
pub enum KnowledgeEntry {
Normal(Knowledge),
Entry(Entry)
}
impl KnowledgeEntry {
pub fn get_entry_mut (&mut self) -> Option<&mut Vec<Box<KnowledgeEntry>>> {
match self {
KnowledgeEntry::Entry(e) => {
Some(&mut e.next)
}
KnowledgeEntry::Normal(k) => {None}
}
}
}
fn main () {
let mut entry = Entry{keyword: "".to_owned(), next: vec![]};
let mut current_container = &mut entry.next;
let new_knowledge = Knowledge{title: "title1".to_owned()};
// new knowledge to be insert in these hierarchy.
let remaining_comp: Vec<_> = vec!["hier1", "hier2", "hier3"];
// each loop will set current_container, either created from new, or found in the vector.
for comp in remaining_comp.iter() {
// true will be replace with body checking with keyword.
let mut _result = current_container.iter_mut().find(|en| {true});
if let Some(mut _res) = _result {
// found the container. Set the current container
if let KnowledgeEntry::Entry(ref mut entry) = &mut **_res {
current_container = &mut entry.next;
} else {
break;
}
} else {
// can't find the resulting container. Create a new vector, and move the container.
let new_container: Vec<Box<KnowledgeEntry>> = vec![];
let mut _temp = KnowledgeEntry::Entry(Entry {
keyword: (*comp).to_string(),
next: new_container
});
current_container.push(Box::new(_temp));
let last_var = current_container.last_mut().unwrap();
// get_entry_mut() to retrieve `_temp` that we have just moved.
current_container = (**last_var).get_entry_mut().unwrap();
}
}
// found the container correspond to "hier1" -> "hier2" -> "hier3", add the entry.
current_container.push(Box::new(KnowledgeEntry::Normal(new_knowledge)));
}
Playground
Produces the following errors:
error[E0499]: cannot borrow `*current_container` as mutable more than once at a time
--> src/main.rs:50:13
|
35 | let mut _result = current_container.iter_mut().find(|en| {true});
| ----------------- first mutable borrow occurs here
...
50 | current_container.push(Box::new(_temp));
| ^^^^^^^^^^^^^^^^^
| |
| second mutable borrow occurs here
| first borrow later used here
error[E0499]: cannot borrow `*current_container` as mutable more than once at a time
--> src/main.rs:51:28
|
35 | let mut _result = current_container.iter_mut().find(|en| {true});
| ----------------- first mutable borrow occurs here
...
51 | let last_var = current_container.last_mut().unwrap();
| ^^^^^^^^^^^^^^^^^
| |
| second mutable borrow occurs here
| first borrow later used here
error[E0499]: cannot borrow `*current_container` as mutable more than once at a time
--> src/main.rs:57:1
|
35 | let mut _result = current_container.iter_mut().find(|en| {true});
| ----------------- first mutable borrow occurs here
...
57 | current_container.push(Box::new(KnowledgeEntry::Normal(new_knowledge)));
| ^^^^^^^^^^^^^^^^^
| |
| second mutable borrow occurs here
| first borrow later used here

How do I conditionally choose between two fields of a struct and then update the chosen one?

I have two structs of the same type inside a larger struct. I will select one of the structs and perform updates using the same code.
A simplified version:
#[derive(Debug)]
struct Counts {
value: u16,
count: u8,
}
#[derive(Debug)]
struct Partitioned {
even: Counts,
odd: Counts,
}
fn sort() -> Partitioned {
let mut even = Counts { value: 2, count: 0 };
let mut odd = Counts { value: 3, count: 0 };
for i in 1..30 {
let mut s = if i % 2 == 0 { even } else { odd };
s.count = s.count + 1;
// ... a lot of other code
}
Partitioned { even, odd }
}
fn main() {
println!("{:?}", sort());
}
This does not compile with complaints about ownership:
error[E0382]: use of moved value: `even`
--> src/main.rs:19:37
|
19 | let mut s = if i % 2 == 0 { even } else { odd };
| ^^^^ value moved here in previous iteration of loop
|
= note: move occurs because `even` has type `Counts`, which does not implement the `Copy` trait
error[E0382]: use of moved value: `odd`
--> src/main.rs:19:51
|
19 | let mut s = if i % 2 == 0 { even } else { odd };
| ^^^ value moved here in previous iteration of loop
|
= note: move occurs because `odd` has type `Counts`, which does not implement the `Copy` trait
error[E0382]: use of moved value: `even`
--> src/main.rs:24:19
|
19 | let mut s = if i % 2 == 0 { even } else { odd };
| ---- value moved here
...
24 | Partitioned { even, odd }
| ^^^^ value used here after move
|
= note: move occurs because `even` has type `Counts`, which does not implement the `Copy` trait
error[E0382]: use of moved value: `odd`
--> src/main.rs:24:25
|
19 | let mut s = if i % 2 == 0 { even } else { odd };
| --- value moved here
...
24 | Partitioned { even, odd }
| ^^^ value used here after move
|
= note: move occurs because `odd` has type `Counts`, which does not implement the `Copy` trait
What is going on here? How can I implement it without copying all the update code to both blocks of the if?
Take a mutable reference to the field:
let mut s = if i % 2 == 0 { &mut even } else { &mut odd };

Resources