一、Rust - 安裝 ;

安裝

Rustup:Rust安裝器和版本管理工具

  要安裝 Rust 的主要方式是經過 Rustup 這一工具,它既是一個 Rust 安裝器又是一個版本管理工具。bash

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

Cargo:Rust 的構建工具和包管理器

  在安裝 Rustup 時,也會安裝 Rust 構建工具和包管理器的最新穩定版,即 CargoCargo 能夠作不少事情:curl

  • cargo build 能夠構建項目
  • cargo run 能夠運行項目
  • cargo test 能夠測試項目
  • cargo doc 能夠爲項目構建文檔
  • cargo publish能夠將庫發佈到crates.io

要檢測是否安裝了RustCargo,能夠在終端運行:cargo --version函數

建立第一個項目

  在終端輸入cargo new hello-rust會生成一個名爲 hello-rust 的新目錄,其中包含如下文件:工具

hello-rust
|- Cargo.toml
|- src
  |- main.rs
複製代碼

  Cargo.tomlRust 的清單文件。其中包含了項目的元數據和依賴庫。測試

  src/main.rs 爲編寫應用代碼的地方。ui

  在進入項目運行cargo run會編譯並運行main函數:url

$ cargo run
  Compiling hello-rust v0.1.0 (/workspace/rust/hello-rust)
    Finished dev [unoptimized + debuginfo] target(s) in 1.93s
     Running `target/debug/hello-rust`
Hello, world!
複製代碼

添加依賴

  能夠爲庫添加依賴。能夠在 crates.io,即 Rust 包的倉庫中找到全部類別的庫。在 Rust 中,一般把包稱做cratesspa

  在 Cargo.toml 文件中添加如下信息:debug

[dependencies]
ferris-says = "0.1"
複製代碼

  接着運行cargo build ,Cargo 就會安裝該依賴。 運行此命令會建立一個新文件 Cargo.lock,該文件記錄了本地所用依賴庫的精確版本。code

  要使用該依賴庫,打開 main.rs,刪除其中全部的內容,而後在其中添加下面這行代碼:

use ferris_says::say;
複製代碼

  這樣就能夠使用 ferris-says crate 中導出的 say 函數了。

相關文章
相關標籤/搜索