Thanks for your help. I wonder if you could help me understand whats happening here? Out of all of those options. The only one that would compile for me was the second variation using Arc which I applied like so:
use std::sync::Arc;
#[async_std::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut futures: Vec<_> = vec![];
let mut my_vecs: Vec<i32> = vec![1,2,3,4];
let vec = std::mem::take(&mut my_vecs);
let shared: Arc<Vec<i32>> = Arc::new(vec);
for i in 0..shared.len()
{
let another_vec = shared.clone();
let handel = async_std::task::spawn(async move {
let b = another_vec[i] + 1;
});
futures.push(handel);
}
for future in futures {
future.await;
}
Arc::try_unwrap(shared).unwrap();
Ok(())
}
The this will compile and run. However this:
#[async_std::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut futures: Vec<_> = vec![];
let my_vecs: Vec<i32> = vec![1,2,3,4];
let shared: Arc<Vec<i32>> = Arc::from(my_vecs);
for i in 0..my_vecs.len()
{
let another_vec = shared.clone();
let handel = async_std::task::spawn(async move {
let b = another_vec[i] + 1;
});
futures.push(handel);
}
for future in futures {
future.await;
}
Ok(())
}
would again complain of borrowing:
error[E0382]: borrow of moved value: `my_vecs`
--> src/arc_method.rs:12:17
|
9 | let my_vecs: Vec<i32> = vec![1,2,3,4];
| ------- move occurs because `my_vecs` has type `Vec<i32>`, which does not implement the `Copy` trait
10 | let shared: Arc<Vec<i32>> = Arc::from(my_vecs);
| ------- value moved here
11 |
12 | for i in 0..my_vecs.len()
| ^^^^^^^ value borrowed here after move
and neither of the ones using concurrency primitives would work at all. These are the ones I've tried:
use futures::stream::{futures_unordered::FuturesUnordered, StreamExt};
async fn handlethis(a_vec: &i32)
{
let b = a_vec + 1;
}
#[async_std::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut futures = FuturesUnordered::new();
let mut my_vecs: Vec<i32> = vec![1,2,3,4];
let et_vecs = &mut my_vecs;
for a_vec in et_vecs
{
let future = handlethis(a_vec);
futures.push(future);
}
while let Some(()) = futures.next().await {}
Ok(())
}
gives:
error[E0597]: `my_vecs` does not live long enough
--> src/futures_method.rs:14:19
|
14 | let et_vecs = &mut my_vecs;
| ^^^^^^^^^^^^ borrowed value does not live long enough
...
24 | }
| -
| |
| `my_vecs` dropped here while still borrowed
| borrow might be used here, when `futures` is dropped and runs the `Drop` code for type `FuturesUnordered`
|
= note: values in a scope are dropped in the opposite order they are defined
and
use futures::stream::StreamExt;
async fn handlethis(a_vec: &i32)
{
let b = a_vec + 1;
}
#[async_std::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut my_vecs: Vec<i32> = vec![1,2,3,4];
let et_vecs = &mut my_vecs;
futures::stream::iter(et_vecs)
.map(|a_vec| handlethis(a_vec))
.buffer_unordered(10)
.for_each(|()| {})
.await;
Ok(())
}
gives:
error[E0277]: `()` is not a future
--> src/futures_method_two.rs:16:6
|
16 | .for_each(|()| {})
| ^^^^^^^^ `()` is not a future
|
= help: the trait `futures::Future` is not implemented for `()`
error[E0277]: `()` is not a future
--> src/futures_method_two.rs:13:5
|
13 | / futures::stream::iter(et_vecs)
14 | | .map(|a_vec| handlethis(a_vec))
15 | | .buffer_unordered(10)
16 | | .for_each(|()| {})
17 | | .await;
| |__________^ `()` is not a future
|
= help: the trait `futures::Future` is not implemented for `()`
= note: required because of the requirements on the impl of `futures::Future` for `ForEach<BufferUnordered<futures::stream::Map<futures::stream::Iter<std::slice::IterMut<'_, i32>>, [closure@src/futures_method_two.rs:14:10: 14:35]>>, (), [closure@src/futures_method_two.rs:16:15: 16:22]>`
= note: required by `futures::Future::poll`