Beginner’s post of trust language

Time:2021-8-2

introduction

Quickly configure the rust development environment and write a small application!

Install rust

You can try rust online on rust playground without installing anything on your computer.

Without installation, try rust directly


Rustup: rust installer and version management tool

The main way to install rust is through rustup, which is both a rust installer and a version management tool.

You appear to be running MacOS, Linux, or other UNIX like systems. To download rustup and install rust, run the following command in the terminal and follow the instructions on the screen.

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

Learn more about installation details


Cargo: rust’s build tool and package manager

When you install rustup, you will also install the latest stable version of the rust build tool and package manager, cargo. Cargo can do many things:

  • cargo buildYou can build projects
  • cargo runCan run project
  • cargo testCan test items
  • cargo docYou can build documents for your project
  • cargo publishYou can publish libraries tocrates.io

To check whether you have installed rust and cargo, you can run in the terminal:

cargo --version

Read the book of cargo


Other tools

Rust supports several editors:

VS CODE

SUBLIME TEXT 3

ATOM

INTELLIJ IDEA

ECLIPSE

VIM

EMACS

GEANY

You can userustup component add rustfmtInstall the code formatting tool rustfmt withrustup component add clippyInstall the lint tool Clippy.

Create a new project

We will write a small application in the new rust development environment. First, create a new project with cargo. Execute in your terminal:

cargo new hello-rust

This generates a file namedhello-rustA new directory containing the following files:

hello-rust
|- Cargo.toml
|- src
  |- main.rs

Cargo.tomlThe manifest file for rust. It contains the metadata and dependency Library of the project.

src/main.rsA place to write application code for.


cargo newA new “Hello, world!” will be generated project We can enter the newly created directory and execute the following command to run this program:

cargo run

You should see the following in the terminal:

$ cargo run
   Compiling hello-rust v0.1.0 (/Users/ag_dubs/rust/hello-rust)
    Finished dev [unoptimized + debuginfo] target(s) in 1.34s
     Running `target/debug/hello-rust`
Hello, world!

Add dependency

Now let’s add dependencies to the application. You cancrates.io, that is, all categories of libraries are found in the warehouse of the rust package. In rust, we usually call packages “crates”.

In this project, we use the nameferris-saysLibrary for.

We areCargo.tomlAdd the following information to the file (obtained from the crite page):

[dependencies]
ferris-says = "0.1"

Then run:

cargo build

… then cargo will install the dependency.

Running this command creates a new fileCargo.lock, which records the exact version of the dependent libraries used locally.

To use this dependency library, we can openmain.rs, delete everything (it’s just an example), and then add the following line of code:

use ferris_says::say;

So we can useferris-saysExported in critesayFunction.

A rust app

We now write a small application with a new dependency library. staymain.rsAdd the following code to:

use ferris_says::say; // from the previous step
use std::io::{stdout, BufWriter};

fn main() {
    let stdout = stdout();
    let out = b"Hello fellow Rustaceans!";
    let width = 24;

    let mut writer = BufWriter::new(stdout.lock());
    say(out, width, &mut writer).unwrap();
}
    

After saving, we can enter the following command to run this application:

cargo run

If everything is correct, you will see that the application prints the following to the screen:

----------------------------
| Hello fellow Rustaceans! |
----------------------------
              \
               \
                 _~^~^~_
             \) /  o o  \ (/
               '_   -   _'
               / '-----' \
    

Learn more!

You are already a rustacean! welcome! We’re glad you joined us! When you are ready, jump to the learning page, where you can find a large number of documents that can help you continue your rust journey.

Learn more!

Who is this crab? Ferris ?

Ferris is the unofficial mascot of the rust community. Many rust programmers call themselves “rustaceans”, which is related to“crustacean”Similar. We use pronouns such as “they” and “them” instead of gender pronouns to refer to Ferris.

Ferris is similar to the adjective “ferrous”, and its meaning is related to iron. Since rust is usually made of iron, it is an interesting source of mascot names.

You canhttp://rustacean.net/Find more pictures of Ferris on.Beginner's post of trust language