安裝
curl https://sh.rustup.rs -sSf | sh
source $HOME/.cargo/env
檢查安裝
cargo --version
創建新項目
cargo new hello-rust
運行
cd hello-rust/
cargo run
結果
Hello, world!
添加依賴
vim Cargo.toml
[dependencies]
ferris-says = "0.1"
安裝依賴
cargo build
編輯 src/main.rs
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();
}
運行
cargo run
結果
----------------------------
| Hello fellow Rustaceans! |
----------------------------
\
\
_~^~^~_
\) / o o \ (/
'_ - _'
/ '-----' \
參考
https://www.rust-lang.org/zh-CN/learn/get-started