Introduction to Rust
π Rust website
The installation
The following is based on MAC πWindows and other installations
Rust installer and package management tool
Curl –proto ‘= HTTPS ‘– tlsv1.2-ssf
https://sh.rustup.rs | sh
π Failed to execute the command. Go to π to install the image
export RUSTUP_DIST_SERVER=
https://mirrors.ustc.edu.cn/r…
export RUSTUP_UPDATE_ROOT=
https://mirrors.ustc.edu.cn/r…
curl
https://sh.rustup.rs -sSf | sh
The results are as follows π
~ β€ curl https://sh.rustup.rs - sSf | sh info: downloading installer Welcome to Rust! This will download and install the official compiler for the Rust programming language, and its package manager, Cargo. Rustup metadata and toolchains will be installed into the Rustup home directory, located at: ... You can uninstall at any time with rustup self uninstall and these changes will be reverted. Current installation options: default host triple: x86_64-apple-darwin default toolchain: stable (default) profile: default modify PATH variable: yes 1) Proceed with installation (default) 2) Customize installation 3) Cancel installation
Select input 1 for stable version π appear the following code is complete
. To configure your current shell, run: source $HOME/.cargo/env
Then run π to save the configuration
source $HOME/.cargo/env
update
rustup update
Cargo Rust’s build tool and package manager
Cargo Run // Cargo test // Cargo doc // Cargo publish // Cargo run // Cargo test // Cargo doc // Cargo publish // Crates. IO. Cargo --version // To check if you have Rust and cargo installed
Start a project
Create a project
cargo new hello-rust
Hello - rust | - Cargo. Toml / / rust manifest file. Contains the project metadata and dependent libraries | - SRC | - main. Rs / / write application code
Run the project
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 the dependent
πrust warehouse Cargo. Toml under [dependencies] add dependencies
[dependencies] - says = "0.2"
Cargo build // Install dependencies
Running this command creates a new file, Cargo. Lock, that records the exact version of the dependency libraries used locally
If a dependency fails to be installed, you can configure an image
CD ~/. Cargo // Enter the cargo root directory,
See Resources for Windows
Cat > config // To create a config file, enter π code directly
[source.crates-io]
registry = "https://github.com/rust-lang/crates.io-index"
replace-with = 'ustc'
[source.ustc]
registry = "git://mirrors.ustc.edu.cn/crates.io-index"
Press Ctrl + D to save
After that, it is ok to execute cargo build again
Use a dependency
Delete the code from main.rs and write the code π
use ferris_says::say; // You can use the say function from Ferris-says Crate
Creating an application
main.rs
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(); }
cargo run
The resources
Rust install the Mac-cargo image. Configure the Windows-cargo image