系列文章目錄
【跟小嘉學(xué) Rust 編程】一、Rust 編程基礎(chǔ)
前言
本系列旨在分享 Rust 學(xué)習(xí)心得,適合初學(xué)者入門,后續(xù)系列會(huì)有 Rust 項(xiàng)目實(shí)戰(zhàn)系列編程介紹。
主要教材參考 《The Rust Programming Language》
一、Rust是什么?
Rust 是一門新的編程語言,它可以讓每個(gè)人編寫可靠且高效的程序,使用于需要運(yùn)行時(shí)速度、需要內(nèi)存安全、更好的l利用多處理器的場(chǎng)景,適合用于做 cli apps、Web Servers等開發(fā)。
Linux 內(nèi)核慢慢在集成 rust的支持;
Rust 特別擅長的領(lǐng)域:
- 高性能web service;
- Web Assembly
- 命令行工具
- 網(wǎng)絡(luò)編程
- 嵌入式設(shè)備
- 系統(tǒng)編程
二、Rust 開發(fā)環(huán)境搭建
2.1、下載地址
Rust 的編譯具依賴于 C 語言的編譯工具,意味著你的電腦至少存在一個(gè) C 語言的編譯環(huán)境,如果你使用的 Linux 環(huán)境,往往已經(jīng)具備了 GCC 或者 clang。
下載地址:https://www.rust-lang.org/tools/install
如果你是 Windows 環(huán)境,我建議實(shí)用 Linux 虛擬機(jī)來學(xué)習(xí),避免出現(xiàn)問題,當(dāng)然如果你熟悉也可以使用試著下列步驟
2.2、Windows 環(huán)境安裝 可以參考
https://www.runoob.com/rust/rust-setup.html
2.3、Mac 環(huán)境安裝
Mac 環(huán)境需要 Xcode;
2.3.1、安裝步驟
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
2.3.2、執(zhí)行完上述命令之后,有如下提示
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) # 默認(rèn)
2) Customize installation # 自定義安裝
3) Cancel installation # 取消安裝
2.4、安裝完成之后檢查
rustc -V # 查看 rust 編譯器的版本
cargo -V # 查看 cargo 的版本
現(xiàn)實(shí)結(jié)果如圖所示
我們開發(fā)工具使用 VSCODE,需要安裝幾個(gè)插件。
- Rust Extension Pack
- Rust Syntax
- Native Debug
三、Rust 提供的命令介紹
# 更新 rust
rustup update
# 卸載
rustup self uninstall
# 查看 rust 版本號(hào)
rustc --version
rustc -V
# 查看本地 rust 文檔
rustup doc
# 代碼格式化工具
rustfmt
四、Hello World
4.1、創(chuàng)建目錄和 main.rs 文件
需要注意 rust 代碼文件以 .rs
結(jié)尾文章來源:http://www.zghlxwxcb.cn/news/detail-495847.html
# 1、創(chuàng)建目錄
mkdir -p ~/Desktop/code/rust_code/helloworld
# 2、切換工作目錄
cd ~/Desktop/code/rust_code/helloworld
# 3、創(chuàng)建文件
touch main.rs
4.2、 編輯 main. rs 文件
fn main(){
println!("Hello, world!");
}
4.3、編譯運(yùn)行
# 1、編譯
rustc main.rs
# 2、運(yùn)行 執(zhí)行結(jié)果在 終端輸出 Hello, world!
./main
五、包管理工具(cargo)
5.1、Cargo 工具的介紹
# 1、查看版本
cargo --version
# 2、創(chuàng)建項(xiàng)目
cargo new hello
# 3、編譯
cd hello
cargo build
# 4、運(yùn)行
cargo run
# 5、檢查代碼語法
cargo check
# 6、編譯release 版本
cargo build --release
# 7、更新依賴版本
cargo update
5.2、Rust 項(xiàng)目目錄結(jié)構(gòu)介
目錄 | 描述 |
---|---|
src | 源代碼目錄 |
src/main.rs | 主程序入口文件 |
target | 目標(biāo)路徑 |
.gitignore | git 忽略文件 |
.Cargo.lock | cargo包管理的文件鎖 |
Cargo.toml | toml文件定義了包的名字、版本、編輯時(shí)間,以及依賴的包版本信息等 |
總結(jié)
以上就是今天要講的內(nèi)容文章來源地址http://www.zghlxwxcb.cn/news/detail-495847.html
- 1、本文介紹了 rust 開發(fā)環(huán)境的安裝與基本命令(rustc/rustup)
- 2、第一個(gè)程序的編譯和運(yùn)行(rustc);
- 3、包管理工具(cargo)的使用
到了這里,關(guān)于【跟小嘉學(xué) Rust 編程】一、Rust 編程基礎(chǔ)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!