title: Why is a PNG image downloaded by reqwest corrupt?
body: <p>I am following the code mentioned in Rust Cookbook at <a href="https://rust-lang-nursery.github.io/rust-cookbook/web/clients/download.html" rel="nofollow noreferrer">https://rust-lang-nursery.github.io/rust-cookbook/web/clients/download.html</a> to download a file in async way by HTTP GET request.</p>
  <p>My code is as follows:</p>
  <pre><code>#[tokio::main]
  async fn main() -&gt; Result&lt;()&gt; {
  
      let object_path = &quot;logos/rust-logo-512x512.png&quot;;
      let target = format!(&quot;https://www.rust-lang.org/{}&quot;, object_path);  
      let response = reqwest::get(&amp;target).await?;
  
      let mut dest = {
      
          let fname = response
              .url()
              .path_segments()
              .and_then(|segments| segments.last())
              .and_then(|name| if name.is_empty() { None } else { Some(name) })
              .unwrap_or(&quot;tmp.bin&quot;);
              
              
          println!(&quot;file to download: '{}'&quot;, fname);
  
          let object_prefix = &amp;object_path[..object_path.rfind('/').unwrap()];
          let object_name = &amp;object_path[object_path.rfind('/').unwrap()+1..];
          let output_dir = format!(&quot;{}/{}&quot;, env::current_dir().unwrap().to_str().unwrap().to_string(), object_prefix);
          fs::create_dir_all(output_dir.clone())?;
  
          println!(&quot;will be located under: '{}'&quot;, output_dir.clone());
                  
          let output_fname = format!(&quot;{}/{}&quot;, output_dir, object_name);
          println!(&quot;Creating the file {}&quot;, output_fname);
          
          File::create(output_fname)?
          
      };
      let content =  response.text().await?;
      copy(&amp;mut content.as_bytes(), &amp;mut dest)?;
      Ok(())
  }
  </code></pre>
  <p>It creates the directories &amp; downloads the file.
  However, when I open the file, it displays corrupt file error
  I have tried to use some other URL also, but the corrupt file issue is still there</p>
  <p>Am I missing something in the code?</p>