I am trying to download a video using rustube
and showing the progress using indicatif
, but when i try i get :
error[E0382]: use of moved value: `progress_bar`
--> src\assets.rs:60:58
|
56 | ...et progress_bar = ProgressBar::new(stream.content_length().awai...
| ------------ move occurs because `progress_bar` has type `ProgressBar`, which does not implement the `Copy` trait...
59 | ... .connect_on_progress_closure_slow(move |arg| progress_bar.in...
| ---------- ------------ variable moved due to use in closure
| |
| value moved into
closure here
60 | ...allback.connect_on_complete_closure(|_| progress_bar.finis...
| ^^^^^^^^ ------------ use occurs due to use in closure
| |
| value used here after move
For more information about this error, try `rustc --explain E0382`.
This is the current function that i am using
async fn check_download_vid_with_id(id : &str) -> Result<(),AssetsError> {
let _dir = format!("{VIDEOS_DIR}/{id}.mp4");
let path = Path::new(&_dir);
match path.exists() {
true => Ok(()),
false => {
let _id = Id::from_str(id).unwrap();
let _video = Video::from_id(_id.into_owned()).await.unwrap();
match _video.best_video() {
None => Err(AssetsError::VideoStreamUnavailable),
Some(stream) => {
let progress_bar = ProgressBar::new(stream.content_length().await.unwrap());
let mut callback = Callback::new()
.connect_on_progress_closure_slow(move |arg| progress_bar.inc(arg.current_chunk as u64));
callback.connect_on_complete_closure(|_| progress_bar.finish() );
stream.download_to_dir(VIDEOS_DIR).await.map(|_| Ok(()))?
}
}
}
}
}
I have to use connect_on_progress_closure_slow(move ..)
as it says closure may outlive progress_bar
. Then how can i do progress_bar.finish()
on download complete.
I have even tried something like
let progress_bar = ProgressBar::new(stream.content_length().await.unwrap());
let callback = Callback::new().connect_on_progress_closure_slow(move |arg| progress_bar.inc(arg.current_chunk as u64));
stream.download_to_dir(VIDEOS_DIR).await.map(|_| {
progress_bar.finish();
Ok(())
})?
But i get :
error[E0382]: borrow of moved value: `progress_bar`
--> src\assets.rs:59:66
|
56 | ...et progress_bar = ProgressBar::new(stream.content_length().await.unwrap());
| ------------ move occurs because `progress_bar` has type `ProgressBar`, which does not implement the `Copy` trait
57 | ...
58 | ...et callback = Callback::new().connect_on_progress_closure_slow(move |arg| progress_bar.in...
| ---------- ------------ variable moved due to use in closure
| |
| value moved into closure here
59 | ...tream.download_to_dir(VIDEOS_DIR).await.map(|_| {
| ^^^ value borrowed here after move
60 | ... progress_bar.finish();
| ------------ borrow occurs due to use in closure
For more information about this error, try `rustc --explain E0382`.