Rust 是由 Mozilla 主導開發
Rust 本來是 Mozilla 員工 Graydon Hoare 的 side project
後來 Mozilla 開始支持這個專案,在 2010 年公開
並於 2015 年釋出第一個穩定版本
截至 2023 年 8 月已釋出 1.71.1 穩定版
Rust 的主打特色讓他受到社群的歡迎
Rust 的準則以 實用、安全 為主
安全: Rust 採取許多措施來預防錯誤,像是使用強型別來嚴格檢查,減少編譯的錯誤
實用: Rust 有標準函式庫,裡面包含了我們會開發到的語法,以及套件打包工具,加速開發人員的開發速度
本系列文章將以 MacOS 為主
官方推薦使用 rustup 來安裝, Window 需要另行安裝
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | 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:
/Users/zhangjianing/.rustup
This can be modified with the RUSTUP_HOME environment variable.
The Cargo home directory is located at:
/Users/zhangjianing/.cargo
This can be modified with the CARGO_HOME environment variable.
The cargo, rustc, rustup and other commands will be added to
Cargo's bin directory, located at:
/Users/zhangjianing/.cargo/bin
This path will then be added to your PATH environment variable by
modifying the profile files located at:
/Users/zhangjianing/.profile
/Users/zhangjianing/.bash_profile
/Users/zhangjianing/.bashrc
/Users/zhangjianing/.zshenv
You can uninstall at any time with rustup self uninstall and
these changes will be reverted.
Current installation options:
default host triple: aarch64-apple-darwin
default toolchain: stable (default)
profile: default
modify PATH variable: yes
1) Proceed with installation (default)
2) Customize installation
3) Cancel installation
沒有特別要設定就選 1 吧
>1
info: profile set to 'default'
info: default host triple is aarch64-apple-darwin
info: syncing channel updates for 'stable-aarch64-apple-darwin'
781.5 KiB / 781.5 KiB (100 %) 699.0 KiB/s in 1s ETA: 0s
info: latest update on 2023-08-03, rust version 1.71.1 (eb26296b5 2023-08-03)
info: downloading component 'cargo'
5.0 MiB / 5.0 MiB (100 %) 1.1 MiB/s in 4s ETA: 0s
info: downloading component 'clippy'
1.9 MiB / 1.9 MiB (100 %) 1.1 MiB/s in 1s ETA: 0s
info: downloading component 'rust-docs'
13.6 MiB / 13.6 MiB (100 %) 1.6 MiB/s in 10s ETA: 0s
info: downloading component 'rust-std'
23.7 MiB / 23.7 MiB (100 %) 1.9 MiB/s in 14s ETA: 0s
info: downloading component 'rustc'
52.6 MiB / 52.6 MiB (100 %) 3.3 MiB/s in 27s ETA: 0s
info: downloading component 'rustfmt'
1.4 MiB / 1.4 MiB (100 %) 1.0 MiB/s in 2s ETA: 0s
info: installing component 'cargo'
info: installing component 'clippy'
info: installing component 'rust-docs'
13.6 MiB / 13.6 MiB (100 %) 7.2 MiB/s in 1s ETA: 0s
info: installing component 'rust-std'
23.7 MiB / 23.7 MiB (100 %) 19.2 MiB/s in 1s ETA: 0s
info: installing component 'rustc'
52.6 MiB / 52.6 MiB (100 %) 21.6 MiB/s in 2s ETA: 0s
info: installing component 'rustfmt'
info: default toolchain set to 'stable-aarch64-apple-darwin'
stable-aarch64-apple-darwin installed - rustc 1.71.1 (eb26296b5 2023-08-03)
Rust is installed now. Great!
To get started you may need to restart your current shell.
This would reload your PATH environment variable to include
Cargo's bin directory ($HOME/.cargo/bin).
To configure your current shell, run:
source "$HOME/.cargo/env"
安裝完畢就可以確認安裝的版本(記得要重開終端機)
> rustc --version
# rustc 1.71.1 (eb26296b5 2023-08-03)
我們先來做一個 rs 檔案吧
$ touch hello.rs
打開 hello.rs 檔案,輸入下列這段程式碼後存檔
fn main() {
println!("Hello, World!");
}
Rust 是編譯語言,在進入執行其之前需要先編譯成電腦看得懂的語言,
所以我們用 rustc 來編譯
rustc hello.rs
接著執行這個檔案
./hello.rs
就會出現 Hello, World!
囉
為了防止程式語言寫得過於艱深,或者是比較特殊的功能
我們會用註解 (comments) 來解釋程式碼
在 Rust 中,該怎麼下註解呢?
// 這是單行註解
/*...*/ 這是多行註解
以剛剛的 hello.rs 來示範單行註解
fn main() {
// first, say hello to the world.
println!("Hello, World!");
}
那多行註解呢?
fn main() {
/*
I'm beginner of rust,
but I'll work hard!
*/
println!("Hello, World!");
}
寫好再重新編譯,結果依然是印出 Hello, World! 唷
在 Rust 中,要印出東西,可以用兩種 macro,
分別是 print! 跟 pringln!
我們直接在 hello.rs 中試試
fn main() {
print!("Hello,");
print!("World!");
println!("Hello,");
println!("World!");
}
一樣編譯後再執行,這時候終端機的結果是這樣:
Hello,World!Hello,
World!
由上述結果可知道 print!
不會換行ln
會在後面加上換行的動作
所以 println!
會換行
今天介紹的都比較基本,我們明天就要進入到變數跟基本語法囉!