Machine Learning

Run pass: Installation and your first CLI device – the original guide

A fixed language of programs in recent years as it includes high and quality security and can be used in many programs. It includes the good features of C and C ++ with modern syntax and the simplest of organizations such as Python. In this article, we will take a step-up step installation to various operating systems and set the line line to understand the formation and operation of the rust line.

To wear stake – action by step

No matter whatever app operating system, it is very easy to enter decay due to a formal installation rustupavailable free from the rusty website. This means that the installation takes only a few steps and varies slightly about various apps.

To add rust under Windows

In Windows, the installer is completely controls installation, and you can follow the steps below:

  1. Go to the “Install” clause of the official Grust website (and download rustup-init.exe file there. The website recognizes the effective applicable program for appropriate program proposals.
  2. As soon as the download is completed, the rustup-init.exe The file can be killed. The command line with various instructions to install and open.
  3. Press the ENTER key to use normal installation to install rust. This includes the following tools:
    • rustc Is the congregator, which includes the code and tested errors before being killed.
    • cargo Is the intersection tool and the package.
    • rustup It is a version manager.

After successful installation, rust should be automatically available for your PATH. This can be easily tested in PowerShll or CMD using the following instructions:

rustc --version cargo --version

If “rust” and “load” however, if the command is not available, it may be due to environmental variation. To view these, you can follow the “PC -> Buildings -> Advanced program settings -> Environmental settings”. There, you must make sure the rusty way, for example “C: username .cargo bin”, is located in the PATH Variable.

To add rust under personality / linux

Linux, rust can be completely put in the end without downloading anything from the rusty website. To add rust, these following steps should be done:

  1. Turn the terminal, for example, with a combination of key CTRL + + Al + T.
  2. To apply rust, the following command is made:
curl --proto '=https' --tlsv1.2 -sSf  | sh

3. You will be asked if the installation should be started. This can be confirmed by installing “1” (default), for example. All packages are required and downloaded, and the environment set.
4. We may have to set a way manually. In this case, you can use this command, for example:

source $HOME/.cargo/env

After the installation is completed, you can check if everything works well. To do this, you can clearly show the Rurturc and Cargo types:

rustc --version cargo --version

To add rusts under Macos

There are several ways to install rust in Macos. If you entered HomeBrew, you can only use this to install the rust by performing the following command:

brew install rustup rustup-init

Alternatively, you can also enter the rust directly using this document:

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

During the installation, you will be asked if you want normal installation. You can just confirm this by pressing the ENTER key. Regardless of the unique selection, you can view the installation by displaying the rusty version to ensure that everything is activated:

rustc --version 
cargo --version

To create a project of rust

At the time of the rust of stability, you may have already appeared cargo The system. This is the official package manager and to formulate rusty system and compare with pip Eppython. cargo Do the following jobs, among others:

  • The implementation of the project
  • Dependence management
  • Collecting code
  • The killing of tests
  • Effective Using Build

This allows you to carry out full projects with rust without dealing with complex construction documents. It also helps you set as soon as possible and easily, which can be filled with health.

In our example, we will build a new project. To do this, we go to the terminal and wander into the folder where we want to save. Then we make the following command to make a new rusting project:

cargo new json_viewer --bin

We call this project json_viewer Because we create a CLL tool that can be used to open and process JSON files. This page --bin The option shows that we want to create a plan that is possible and not a library. Now you have to be able to see the following folder structure on your identifier after making a command:

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

Every new project has this building. Cargo.toml It contains all the dependence and medadata of your project, such as name, libraries used, or version. This page src/main.rsOn the other hand, in time, it contains a real rust code, which described the steps made when the program started.

First, we can explain a simple task here that produces a result of terminal:

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

The system can be easily called from terminal using cargo:

cargo run

With this call to work, it should be assured that you are in the main indication of the project, that is Cargo.toml The file is stored. If everything is planned and done well, you will receive this output:

Hello, Rust CLI-Tool!

Through these few steps, you just created your most effective first-running project, which we can create in the following section.

Building a CLI Tool: Simple Jon Parser

We now begin to fill out the project through life and create a program that can read JSON files and remove their content from the paralysis in an organized manner.

The first step is to explain reliance, that is, libraries to use between the project. These are stored in Cargo.toml file. With the stability, called crates that are compared to information libraries or modules that provide some effective performance. For example, they can have a re-used code written by some engineers.

We need the following crates for our project:

  • serde It enables the serialization and recycling of data formats such as JSON or Yaml.
  • serde_jsonOn the other hand, extension is specially constructed working with JSON files.

To get your project to access these crates, they must be stored in the Cargo.toml file. This looks like this after creating the project:

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

[dependencies]

We can now add the necessary crates to [dependencies] part. Here we also explain the type to be used:

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

Ensure that additional dependability is available in this function, it should be started and constructed. To do this, the next terminal command can be executed in a great guide:

cargo build

During execution, cargo Searching for medieval pounds of return .IO depending on the specified self-maintaining versions. These crates are included in the code and stored so that no recycling is to create the following.

If these steps work, we are now ready to write the actual rust code that opens and processing JSON file. To do this, you can open the src/main.rs File and restore 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() < 2 {
        println!(“Please specify the path to your file.”);
        return;
    }

    // Read in File
    let file_path = &args[1];
    let file_content = fs::read_to_string(file_path)
        .expect(“File could not be read.”);

    // Parse JSON
    let json_data: Value = serde_json::from_str(&file_content)
        .expect(“Invalid JSON format.”);

    // Print JSON
    println!(" JSON-content:n{}”, json_data);
}

Code follows these steps:

  1. View Conflicts:
    • We read arguments from command line with env::args().
    • The user must say the way to JSON's file at first.
  2. Read File:
    • With the help of fs::read_to_string()The file content is read with the string.
  3. PARSE JSON:
    • Crate serde_json It turns the thread into a rust of rusty with a kind value.
  4. Format exit:
    • The content is well-issued to console.

To check the tool, you can, for example, create an assessment file in Project Directory undername examples.json:

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

The program was killed used cargo run And the way to JSON's file is also defined:

cargo run ./example.json

This brings us at the end of our first rust project and we have successfully built a simple CLI tool that can read JSON files and delete them into the command line.

This is what to take with you

  • Installing rust is fast and easy to many apps. Some required items are already included.
  • With the help of cargoAn empty project can be directly created, containing the necessary files and where you can start writing the rust code
  • Before you start the program, you must put on depending on and output using the construction order.
  • Now that all is supported, you can start with a real program.

Source link

Related Articles

Leave a Reply

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

Back to top button