use std::fs::File;
use std::io;
fn main() {
let save_file_path = "temp.zip";
let download_url = "https://github.com/Aaron009/temp_rust_web_download/raw/main/wwwroot/temp.zip";
let resp = reqwest::blocking::get(download_url).expect("request failed");
let body = resp.text().expect("body invalid");
let mut out = File::create(save_file_path).expect("failed to create file");
let mut body_bytes = body.as_bytes();
io::copy(&mut body_bytes, &mut out).expect("failed to copy content");
println!("download completed!");
}
I use "Beyond Comparison" to compare screenshots as follows:
As shown in the figure, it is found that the file size is different, and the file size marked in red is also different. what is the reason?
The reason is simple - downloaded file is not a text, so, when you converted it to String, every non-Unicode characters were replaced by 0xefbfbd sequence, the so-called "replacement character". You probably want to use resp.bytes() instead of resp.text().
I have never seen such a simple question, so I am ashamedć
thank you very muchļ¼
My final code was changed to the following:
use std::fs::File;
use std::io;
fn main() {
let save_file_path = "temp.zip";
let download_url = "https://github.com/Aaron009/temp_rust_web_download/raw/main/wwwroot/temp.zip";
let resp = reqwest::blocking::get(download_url).expect("request failed");
let body = resp.bytes().expect("body invalid");
let mut out = File::create(save_file_path).expect("failed to create file");
let mut body_bytes= body.to_vec();
io::copy(&mut &body_bytes[..], &mut out).expect("failed to copy content");
println!("download completed!");
}
Besides reading the book, I guess you should just try to actually use the language to build something, then ask questions when you get stuck.
I started looking at Rust around five years ago, but at that time it was only a bit at the time. Honestly, answering questions here has been probably my biggest source of learning things about Rust, since I get to see so many different questions from so many different parts of Rust I would not encounter on my own.
When I learn any language, I try to do what I want to do, then look at the problems other people have and see how they solve it. I often go to forums or Stack Overflow.
For me, AFAIR I read the nomicon pretty quickly, too, after the book. Then looking through the standard library (docs) to know the API for common types (especially the data structures) without having to look it up helps, as well looking through the reference for deepening the understanding of the Rust language in detail. Going further, reading (some of) the documentation of some popular crates helps as well. Iām personally not so much into creating my own practical code, hence I didnāt really ask any questions myself for learning Rust; so for me reading other peopleās questions (and then the answers) or perhaps even trying to answer the myself in this forum also aided with learning Rust, I would say. I do write small test programs if Iām wondering what the compiler has to say about some things, so in a playing-around-with-the-compiler kind of fashion I do write code. Another thing is: Thereās this ā[src]ā button on every function or type in std or any crate, so clicking that just reading the implementation of something that seems interesting but not too complicated is a great learning experience, too.
On certain aspects, especially the fundamental concepts behind Rustās type-system, low-level stuff, types with constructors and destructors, etc. was in my case aided by knowing C and C++ a bit and knowing Haskell really well; as well as some general knowledge from my computer science degrees.
And on some topics searching for specific tutorials / talks / etc. online helps, e.g. around async fn.
Just to add on what other people said, I've learned a lot from following This Week in Rust. When you're starting out, you'll probably learn something from most of the linked blog posts and the like. Regardless of where you're at, you'll learn about what's going on in the language from the RFCs, stablization FCPs, and so on. Version announcements/changelogs are good for this too. And if you read the github issues and PRs generally, you'll soon get a feel for who the main contributors are, or at least a feel for many team members. This can pay off later on if you come across a crate and recognize the maintainers, for example.
You can also look up the blogs of team members, forum posters, etc. once you've found some with a presentation that is a good match for your learning style. Here are a few I've learned from that come to mind offhand: