Rust

website

Subsequent updates github.com/guokaigdg/m…

The installation

Install programs and version management tools
// To download Rustup and install Rust on macOS, Linux, or Unix-like operating systems, run the following commands on your terminal and follow the on-screen instructions. // If you are using Windows, see "Other Installation methods". Curl, proto '= HTTPS' - tlsv1.2 - sSf https://sh.rustup.rs | shCopy the code

update

rustup update
Copy the code

Version to view

cargo --version
Copy the code

Cargo: Rust builds tools and package managers

When you install Rustup, you get the latest stable version of the Rust build tool and package manager, called Cargo.

Cargo can do a lot of things:

Cargo Build Build cargo Run Run cargo Test Test cargo DOC build document Cargo Publish publish to crates. IO

Create a new project

Write a small application in the Rust development environment. First, we will use Cargo to make a new project for us.
cargo new hello-rust
Copy the code
A new directory named Hello-rust will be generated containing the following files:
Hello - rust | - Cargo. Toml / / manifest file, used to save the project of metadata, and dependency | - SRC | - main. Rs / / write application codeCopy the code

Run your Rust

Run in the hello-rust root directory
cargo run
Copy the code
The results
The Compiling hello - rust v0.1.0 (/ Users/guokai/Desktop/guokai/Front - end - Development - Notes/rust/hello - rust) Finished dev [unoptimized + debuginfo] target(s) in 1.63s Running 'target/debug/hello-rust' hello, world!Copy the code

Adding dependencies

Let’s add a dependency to the application. You can find various types of libraries in crates. IO, Rust’s package registry. In Rust, we often call packaging crates.

Add Ferris-says to your project.

Added to the hello-rust/Cargo. Toml file

[dependencies] - says = "0.2"Copy the code
Next you can install dependencies
  • A new file, Cargo. Lock, is created. This file is a log of the exact version of the dependency used locally.
cargo build
Copy the code
  • The installation is complete
Updating crates.io index warning: spurious network error (2 tries remaining): error sending data: Broken pipe; Class =Net (12) Downloaded Ferris-says V0.2.1 (smawk) v0.3.1 (Unicode-width v0.1.9 Downloaded 5 Crates (97.7 KB) in 3.52s Unicodewidth v0.1.9 Compiling Compiling: SMAwk v0.3.1 Compiling Smallvec v0.4.5 Compiling Textwrap v0.13.4 Compiling Ferris -says v0.2.1 Compiling Helo-rust V0.1.0 (/ Users/guokai/Desktop/guokai/Front - end - Development - Notes/Rust/hello - Rust) Finished dev [unoptimized + debuginfo]  target(s) in 3m 10sCopy the code

Use a dependency

  • To use this dependency, we can open main.rs, delete everything in it, and add the following line:
use ferris_says::say;
Copy the code

The above code means using the say function

Create a small Rust program

  • Main. rs adds the following code
use ferris_says::say; // from the previous step use std::io::{stdout, BufWriter}; fn main() { let stdout = stdout(); let message = String::from("Hello fellow Rustaceans!" ); let width = message.chars().count(); let mut writer = BufWriter::new(stdout.lock()); say(message.as_bytes(), width, &mut writer).unwrap(); }Copy the code

Save and run

cargo run 
Copy the code

Running result

Assuming all goes well, print something like this on the screen:

Finished dev [unoptimized + debuginfo] target (s) in 0.02 s Running ` target/debug/hello - rust ` __________________________"  Hello fellow Rustaceans! > -------------------------- \ \ _~^~^~_ \) / o o \ (/ '_ - _' / '-----' \Copy the code

New features added github.com/guokaigdg/m…

If you are interested, you can give a star. Let’s play together.