Rust---hello world

安裝

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章