24. March 2021

How to download binary file in Rust by reqwest

reqwest in Rust allows you to download file. The question is how to store the file on the filesystem. There are several examples on the internet where authors are calling .text() which will result in a corrupted file on the filesystem because text() is decoding UTF-8 characters.

Here is an example of how to download a binary file and store content in a file on the local file system.

File download.rs:

use std::io::Cursor;
type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;

async fn fetch_url(url: String, file_name: String) -> Result<()> {
    let response = reqwest::get(url).await?;
    let mut file = std::fs::File::create(file_name)?;
    let mut content =  Cursor::new(response.bytes().await?);
    std::io::copy(&mut content, &mut file)?;
    Ok(())
}

#[tokio::main]
async fn main() {
    fetch_url("https://georgik.rocks/wp-content/uploads/sianim.gif".to_string(), "siriel.gif".to_string()).await.unwrap();
}

File Cargo.toml:

[package]
name = "download"
version = "0.1.0"
authors = ["Georgik.Rocks"]
edition = "2018"

[dependencies]
reqwest = "*"
tokio = { version = "1", features = ["full"] }

[[bin]]
name = "download"
path = "download.rs"

To build the example type following:

cargo run
Facebook Comments

1. March 2021

Lenovo T14 CPU locked at 0.8 GHz on Windows

Users with brand new Lenovo T14 might face strange behavior on Windows. The computer seems to be slow and the detail of CPU usage displays a constant CPU speed set to 0.8 GHz.

The problem can be solved by upgrading BIOS. Drivers can be found at pcsupport.lenovo.com

Facebook Comments

1. December 2020

How to switch tabs in Windows Terminal

Windows Terminal is very versatile shell for Windows which allows to run CMD, PowerShell or Linux in WSL2. The terminal also supports tabs.

To switch between tabs you can use:

  • CTRL+Tab – switch to next tab
  • CTRL+Shift+Tab – switch to previous tab
  • If you’d like to use own keyboard shortcuts like CTRL+PgUp, CTRL+PgDown, then just open Settings CTRL+, and enter following code into section actions:

    ..."actions":  [
     ...
      { "command": "nextTab", "keys": "ctrl+pgdn" },
      { "command": "prevTab", "keys": "ctrl+pgup" }
     ...
    ]
    
    Facebook Comments

1. October 2020

How to display Debug Console with output from the application in Xcode

When you hit Build and Run button in Xcode the application will start.

If everything is ok you should see messages from the application displayed in the lower right part of Xcode window. The Console output contains text printed by the application.

Sometimes it might happen that there is no Console output window. How to fix it?

Here is small tip for Xcode developers.

To show/hide the Console click the icon Show/Hide the console in the lower right corner. It’s the last icon on the lower right side of the panel.

Facebook Comments

21. September 2020

How to install upgrade of Xcode 12 when there are just few GBs free on the mac

Xcode was always a big bundle. Each release was slightly bigger and an user has to remove some software to install the upgrade.

The release of Xcode 12 is way bigger and it requires more than 40 GB of free disk space to install the upgrade.

The question is: How to install Xcode 12 when there is nothing else to delete?

Simply: Go to Launchpad and throw Xcode to Recycle Bin to uninstall the current version of Xcode.

This step will release the space consumed by Xcode 11. The delete process will take several minutes and you can check your disk for free space.

df -h

Once you have sufficient disk space then open App Store, search for Xcode and perform new installation.

Facebook Comments