This commit is contained in:
Ezra Barrow 2021-04-06 13:21:45 -04:00 committed by Ezra Barrow
parent 752b035c1a
commit 5518c161ba

View file

@ -6,12 +6,12 @@ Creates a temporary directory with [`tempfile::Builder`] and downloads
a file over HTTP using [`reqwest::get`] asynchronously.
Creates a target [`File`] with name obtained from [`Response::url`] within
[`tempdir()`] and copies downloaded data into it with [`io::copy`].
[`tempdir()`] and writes downloaded data into it with [`Writer::write_all`].
The temporary directory is automatically removed on program exit.
```rust,edition2018,no_run
use error_chain::error_chain;
use std::io::copy;
use std::io::Write;
use std::fs::File;
use tempfile::Builder;
@ -41,15 +41,15 @@ async fn main() -> Result<()> {
println!("will be located under: '{:?}'", fname);
File::create(fname)?
};
let content = response.text().await?;
copy(&mut content.as_bytes(), &mut dest)?;
let content = response.bytes().await?;
dest.write_all(&content)?;
Ok(())
}
```
[`File`]: https://doc.rust-lang.org/std/fs/struct.File.html
[`io::copy`]: https://doc.rust-lang.org/std/io/fn.copy.html
[`reqwest::get`]: https://docs.rs/reqwest/*/reqwest/fn.get.html
[`Response::url`]: https://docs.rs/reqwest/*/reqwest/struct.Response.html#method.url
[`tempfile::Builder`]: https://docs.rs/tempfile/*/tempfile/struct.Builder.html
[`tempdir()`]: https://docs.rs/tempfile/3.1.0/tempfile/struct.Builder.html#method.tempdir
[`tempdir()`]: https://docs.rs/tempfile/*/tempfile/struct.Builder.html#method.tempdir
[`Writer::write_all`]: https://doc.rust-lang.org/std/io/trait.Write.html#method.write_all