It does, rather confusingly:


Let’s try this in rust.
error[E0502]: cannot borrow `vec` as mutable because
it is also borrowed as immutable
--> src/main.rs:4:22
|
3 | let first_elem = &vec[0];
| --- immutable borrow occurs here
4 | for _ in 1..10 { vec.push(*first_elem); }
| ^^^^^^^^^-----------^
| | |
| | immutable borrow
| | later used here
| mutable borrow occurs here
error: aborting due to previous error
error[E0502]: cannot borrow `vec` as mutable because
it is also borrowed as immutable
--> src/main.rs:4:5
|
3 | let first_elem = &vec[0];
| --- immutable borrow occurs here
4 | vec.push(first_elem.to_string());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ mutable borrow occurs here
5 | println!("{}", first_elem);
| ---------- immutable borrow later used here
#include <bits/stdc++.h>
using namespace std;
vector<int> nums;
void writeToNums() {
for (int i = 0; i < 3; i++)
nums.push_back(i);
}
int main() {
vector<thread> threads;
for (int i = 0; i < 20; i++) threads.emplace_back(writeToNums);
for (auto& th: threads) th.join();
for (auto item: nums) cout << item << ' ';
}Undefined behavior again.
use std::thread;
fn write_to_nums(nums: &mut Vec<i32>) {
for i in 0..3 {
nums.push(i);
}
}
fn main() {
let mut nums = vec![];
let mut threads = vec![];
for _ in 0..20 {
threads.push(thread::spawn(move || {
write_to_nums(&mut nums);
}));
}
for thread in threads {
let _ = thread.join();
}
for num in nums {
println!("{}", num);
}
}error[E0382]: use of moved value: `nums`
--> src/main.rs:13:32
|
9 | let mut nums = vec![];
| -------- move occurs because `nums` has type
| -------- `std::vec::Vec<i32>`, which does
| -------- not implement the `Copy` trait
...
13 | threads.push(thread::spawn(move || {
| ^^^^^^^ value moved into
| closure here, in previous iteration of loop
14 | write_to_nums(&mut nums);
| ^^ use occurs due to use in closure
