This code snippet is part of my local repository for demo and simple applications. I wanted to see a difference between Go and Rust code for the given task.

There are many ways to implement a simple HTTP Get in each language. Since I already have Golang experience, the code in Go took me a few minutes.

My initial goal for the Rust code was to write an HTTP Get using only the standard library. But surprisingly, the Rust code took me at least one hour, where at the end, I just took the code from the reqwest package and simplified it for the text response.

Go implementation

package main

import (
    "io/ioutil"
    "log"
    "net/http"
)

func main() {
    log.Println("mycurl")

    url := "https://kenanbek.github.io/"
    resp, err := http.Get(url)
    if err != nil {
        log.Fatalf("Error fetching url %s: %v", url, err)
    }

    defer resp.Body.Close()
    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        log.Fatalf("Error fetching url %s: %v", url, err)
    }

    log.Println("Response: ")
    log.Println(string(body))
}

Rust implementation

For the Rust implementation, I am going to use reqwest package. That’s why we need to initialize a project environment using the following commands:

cargo init

Then edit content of the Cargo.toml and add reqwest dependency:

[dependencies]
reqwest = { version = "0.11", features = ["blocking", "json"] }

I will use blocking call api.

Here is the actual Rust code for fetching webpage’s content via HTTP GET request:

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let resp = reqwest::blocking::get("https://kenanbek.github.io/")?
        .text()?;
    println!("{:#?}", resp);
    Ok(())
}

If you are also a newbie in Rust as myself, you can run this code by executing the following command:

cargo run