Data Science

Get started with Rust: Install and Your First CLI Tools – Beginner’s Guide

In recent years, it has been a popular programming language because it combines security and high performance and can be used in many applications. It combines the positive features of C++ with the simplicity of modern syntax and other programming languages ​​such as Python. In this article, we will step by step look at the installation of Rust on various operating systems and set up a simple command line interface to understand the structure and functionality of Rust.

Installing Rust – Step by Step

Regardless of the operating system rustupIt is available for free on the Rust website. This means that installation takes only a few steps and is only slightly different for various operating systems.

Install rust under windows

In Windows, the installer has full control over the installation, and you can follow these steps:

  1. Go to the “Installation” section on the official Rust website (and download it rustup-init.exe Filed there. The website identifies the underlying operating system in order to make appropriate suggestions directly regarding the use of the system.
  2. After the download is completed, rustup-init.exe The file can be executed. Then open the command line with various installation instructions.
  3. Press Enter to run the standard installation to install RUST. This also includes the following tools:
    • rustc is a compiler that compiles the code before execution and checks for errors.
    • cargo It is a build and packaging management tool for Rust.
    • rustup It’s version manager.

After successful installation, rust should be in your PATH. This can be easily checked in PowerShell or CMD using the following command:

rustc --version cargo --version

If “Rustc” and “Cargo” are displayed in the output with their respective version numbers, the installation is successful. However, if the command is not found, it may be due to the environment variable. To check these, you can follow the “This PC -> Properties -> Advanced System Settings -> Environment Variables” path. Once you get there, you should make sure that the rusted path, such as “C:Usersusername.cargobin”, exists in PATH Changeable.

Install rust under Ubuntu/linux

In Linux, RUST can be installed completely through the terminal without downloading anything from the Rust website. To install rust, the following steps must be performed:

  1. For example, use the key combination Ctrl + Alt + T to open the terminal.
  2. To install Rust, execute the following command:
curl --proto '=https' --tlsv1.2 -sSf  | sh

3. You will then be asked if the installation should be started. For example, this can be confirmed by entering “1” (the default value). Then download all the required packages and set up the environment.
4. You may have to set the path manually. In this case you can use this command, for example:

source $HOME/.cargo/env

Once the installation is complete, you can check that everything is working properly. To do this, you can explicitly display the versions of Rustc and Cargo:

rustc --version cargo --version

Install rust under macos

There are several ways to install Rust on MacOS. If you already have Homebrew installed, you can use it to install RUST by executing the following command:

brew install rustup rustup-init

In addition, you can also use this script to install Rust directly:

curl --proto '=https' --tlsv1.2 -sSf  | sh

During the installation process, you will be asked if you want to run a standard installation. You can confirm this by pressing Enter. Regardless of which variant you choose, you can make sure everything works by showing the RUST version:

rustc --version 
cargo --version

Create rusty projects with goods

During installation of rust, you may have already encountered it cargo program. This is the official package manager and manufactured rust system that can be used with pip In Python. cargo Perform the following tasks, etc.:

  • Project Initialization
  • Dependency management
  • Compile the code
  • Perform a test
  • Optimization of construction

This allows you to manage Rust’s complete projects without having to deal with complex build scripts. It also helps you build new projects quickly and easily and then fill your life.

In terms of our example, we will create a new project. To do this, we go to the terminal and navigate to a folder where we want to save it. Then, we execute the following command to create a new Rust project:

cargo new json_viewer --bin

We call this project json_viewer Because we are building a CLI tool that can be used to open and process JSON files. this --bin Options indicate that we want to create an executable program instead of a library. After executing the command, you should now be able to see the following folder structure in the directory:

json_viewer/ 
├── Cargo.toml # Project configuration 
└── src 
     └── main.rs # File for Rust Code

Every new project has this structure. Cargo.toml Contains all dependencies and metadata for the project, such as the name, the library or version used. this src/main.rsOn the other hand, the actual rust code is later included, and the steps performed when starting the program are then defined.

First, we can define a simple function here that generates output in the terminal:

fn main() { 
     println!("Hello, Rust CLI-Tool!"); 
}

This program can be easily called using a terminal cargo:

cargo run

For the phone call for this work, you must make sure you are in the main directory of the project, i.e. Cargo.toml File storage. If everything is set and executed correctly, you will receive this output:

Hello, Rust CLI-Tool!

With these few steps, you have just created the first successful Rust project, which we can build in the next section.

Building CLI Tools: Simple JSON Parser

Now, we start populating the project with life and creating a program that can read the JSON file in a structured way and output its contents in a terminal.

The first step is to define the dependencies we will use during the project process, i.e. the libraries we will use. These are stored in Cargo.toml document. In Rust, the so-called crates are comparable to libraries or modules that provide certain predefined functions. For example, they can be made up of reusable code written by other developers.

We need crates for the following items:

  • serde Enable serialization and absolute serialization of data formats such as JSON or YAML.
  • serde_jsonOn the other hand, it is an extension specially designed for using JSON files.

In order for your project to access these crates, they must be stored in Cargo.toml document. After creating the project, this looks like this:

[package] 
name = "json_viewer" 
version = "0.1.0" 
edition = "2021" 

[dependencies]

Now, we can [dependencies] part. Here we also define the version to use:

[dependencies] 
serde = "1.0" 
serde_json = "1.0"

To ensure the added dependencies in the project, they must be downloaded and built first. To do this, you can execute the following terminal commands in the home directory:

cargo build

During execution, cargo Search for the central rusty repository crates for dependencies and specified versions to download them. These crates are then compiled and cached with the code so that you don’t have to download the next build again.

If these steps work, we are now ready to write the actual rust code that opens and processes the JSON file. To do this, you can open src/main.rs File and replace existing content with this code:

use std::fs;
use serde_json::Value;
use std::env;

fn main() {
    // Check arguments
    let args: Vec = env::args().collect();
    if args.len() 

The code follows the following steps:

  1. Check parameters:
    • We read parameters through the command line env::args().
    • The user must specify the path to the JSON file at startup.
  2. Read the file:
    • With the help fs::read_to_string()read the contents of the file as a string.
  3. Parsing JSON:
    • Crate serde_json Converts a string to a rust object with type value.
  4. Format output:
    • Content can be output clearly in the console.

To test the tool, you can create a test file with a name in the project directory examples.json:

{
  "name": "Alice",
  "age": 30,
  "skills": ["Rust", "Python", "Machine Learning"]
}

Then use the program to execute cargo run And also define the path to the JSON file:

cargo run ./example.json

This brings us to the end of Rust’s first project, and we successfully built a simple CLI tool that reads JSON files and outputs them to the command line.

This is what you should carry with you

  • In many operating systems, installing rust is very easy. The other components required are already installed.
  • With the help cargoyou can create an empty project directly with the necessary files where you can start writing Rust Code
  • Before you start programming, you should enter the dependencies and download them using the build command.
  • Now that everything is set up, you can start with actual programming.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button