Skip to content

Instantly share code, notes, and snippets.

@bbatha
Created April 13, 2015 18:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bbatha/9df1fcc1fa0a72a9f57d to your computer and use it in GitHub Desktop.
Save bbatha/9df1fcc1fa0a72a9f57d to your computer and use it in GitHub Desktop.
Arc::new() lifetime error
fn it_works() {
let guards: Vec<thread::JoinGuard<()>> = Vec::with_capacity(3);
{
let mut pool = ScopedThreadPool::new(2);
for _ in (0..3) {
guards.push(pool.execute(move || {
thread::sleep_ms(1000);
println!("Things are happening!");
}));
}
}
println!("Still running");
Errors with:
Compiling threadpool v0.0.1 (file:///home/user/bbatha/projects/threadpool-rs)
src/lib.rs:42:17: 42:23 error: cannot borrow immutable local variable `guards` as mutable
src/lib.rs:42 guards.push(pool.execute(move || {
^~~~~~
src/lib.rs:42:29: 42:33 error: `pool` does not live long enough
src/lib.rs:42 guards.push(pool.execute(move || {
^~~~
src/lib.rs:36:19: 49:6 note: reference must be valid for the block at 36:18...
src/lib.rs:36 fn it_works() {
src/lib.rs:37 let guards: Vec<thread::JoinGuard<()>> = Vec::with_capacity(3);
src/lib.rs:38 {
src/lib.rs:39 let mut pool = ScopedThreadPool::new(2);
src/lib.rs:40
src/lib.rs:41 for _ in (0..3) {
...
src/lib.rs:39:53: 47:10 note: ...but borrowed value is only valid for the block suffix following statement 0 at 39:52
src/lib.rs:39 let mut pool = ScopedThreadPool::new(2);
src/lib.rs:40
src/lib.rs:41 for _ in (0..3) {
src/lib.rs:42 guards.push(pool.execute(move || {
src/lib.rs:43 thread::sleep_ms(1000);
src/lib.rs:44 println!("Things are happening!");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment