Nazarii Sheremet

I'm Software Developer. Working with Ruby/MRuby/Rust/Go/C/C++. Geek. Husband.
Eastern Europe, Ukraine

My Rust Experience

Hi guys.

Two weeks ago I started to learn Rust, for better learning, I decided to rewrite my library mruby-smallhttp on Rust. Let’s get to know Cargo. It’s something like bundler in Ruby world.

$ cargo new library # => generate a template for project
$ tree # =>
library
├── Cargo.toml
└── src
    └── lib.rs

Cargo.toml it’s a manifest file. If you want to know more about how to get started in Rust, here is the link. lib.rs it’s a main file for a library. If you create a binary project the main file is main.rs.

If you want to use my lib in your projects, here is examples:

Install
# Cargo.toml
[dependencies]
knock = "0.1"
Simple GET request
extern crate knock;

use knock::HTTP;

fn main() {
	let http = HTTP::new("https://google.com").unwrap();
	let response = http.get().send();
}
Simple POST request
extern crate knock;

use knock::HTTP;
use std::collections::HashMap;

fn main() {
    let http = HTTP::new("https://google.com").unwrap();
    let mut body = HashMap::new();
    let mut headers = HashMap::new();

    body.insert("file", Data::File("/path/to/file.file"));
    body.insert("field", Data::String("value"));

    headers.insert("Content-Type", "multipart/form-data");

    let response = http.post().body(body).header(headers).send();
}

After writing knock, I wanted to write a command line tool to send an HTTP request. Before that, I used POSTMAN, but I don’t like electron because it’s Chrome. I don’t like chrome at all, but when it needs to be used to work with POSTMAN it’s too much. So I started using my knock lib for writing command-line tool on Rust.

I wrote it quickly, I had to add some code to knock lib. Now I have a command-line tool with name getsb. It’s not over yet, and now version v0.1.0. I want to add more features. So I will very glad for any help.

How to Install
$ git clone https://github.com/nsheremet/getsb-cli.git
$ cd getsb-cli
$ cargo build --release

And then move binary file in bin dir:

Linux
$ sudo mv target/release/getsb /usr/local/bin
OSX
$ sudo mv target/release/getsb /usr/local/bin/getsb
Windows
  • Create a folder for getsb
  • search for env
  • open “edit your enviroment variables”
  • edit PATH
  • append folder path to the end of the string ie: <path_stuff_here>;C:/getsb/;
How to use
$ getsb POST https://example.com/api/data -b "key=value" -h "Content-Type: application/x-www-form-urlencoded"

For more info check README.md file.

After all this, I really liked Rust. This is an awesome language with safe working with memory and other awesome things.

If you want to start to learn here is few links:

And also link on my projects:

So, if you want to learn Rust, here is a very nice book.

Thanks.