-
-
Save matklad/5327876d6e9ae400361e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn main() { | |
let xs = vec![1, 2, 3]; | |
std::thread::spawn(|| { | |
println!("{:?}", xs); | |
}); | |
} | |
// error: closure may outlive the current function, but it borrows `xs`, which is owned by the current function | |
fn main() { | |
let xs = vec![1, 2, 3]; | |
std::thread::spawn(move || { | |
println!("{:?}", xs); | |
}); | |
} | |
use std::sync::mpsc::channel; | |
fn main() { | |
let (tx, rx) = channel(); | |
std::thread::spawn(move || { | |
let xs = rx.recv().unwrap(); | |
println!("{:?}", xs); | |
}); | |
let xs = vec![1, 2, 3]; | |
tx.send(xs).unwrap(); | |
} | |
fn main() { | |
let mut xs = [0, 0, 0, 0]; | |
for i in &mut xs { | |
*i += 1; | |
} | |
println!("{:?}", xs); | |
} | |
extern crate crossbeam; | |
fn main() { | |
let mut xs = [0, 0, 0, 0]; | |
crossbeam::scope(|scope| { | |
for i in &mut xs { | |
scope.spawn(move || { | |
*i += 1; | |
}); | |
} | |
}); | |
println!("{:?}", xs); | |
} | |
extern crate crossbeam; | |
fn main() { | |
let xs = std::sync::Mutex::new([0, 0, 0, 0]); | |
crossbeam::scope(|scope| { | |
for _ in 0..10 { | |
scope.spawn(|| { | |
let mut guard = xs.lock().unwrap(); | |
let xs: &mut [i32; 4] = &mut guard; | |
for i in xs { | |
*i += 1; | |
} | |
}); | |
} | |
}); | |
println!("{:?}", *xs.lock().unwrap()); | |
} | |
extern crate rayon; | |
fn main() { | |
let mut xs = [1, 3, 0, 6, 2, 4, 92]; | |
quick_sort(&mut xs); | |
println!("{:?}", xs); | |
} | |
fn quick_sort(xs: &mut[i32]) { | |
if xs.len() > 1 { | |
let mid = partition(xs); | |
let (lo, hi) = xs.split_at_mut(mid); | |
rayon::join(|| quick_sort(lo), | |
|| quick_sort(hi)); | |
} | |
} | |
fn partition(xs: &mut[i32]) -> usize { | |
// ... | |
} | |
extern crate crossbeam; | |
fn main() { | |
let mut xs = [0, 0, 0, 0]; | |
crossbeam::scope(|scope| { | |
for i in &mut xs { | |
scope.spawn(move || *i += 1 ); | |
scope.spawn(move || *i += 1 ); | |
} | |
}); | |
} | |
error: capture of moved value: `i` [E0382] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment