Rust
官網: https://www.rust-lang.org
版本:nightly、beta、stable 如何設計語言的討論:https://github.com/rust-lang/rfcs
標準庫文檔:https://doc.rust-lang.org/std/
python
Rust安裝
官方的安裝文檔:https://www.rust-lang.org/tools/install
通常有兩種安裝方式:手動下載安裝與使用Rustup工具安裝,推薦使用第二種方式安裝。 Rustup下載地址:https://rustup.rs/ windows下能夠雙擊運行,linux 下能夠執行 curl https://sh.rustup.rs -sSf | sh
linux
u@DESKTOP-JSC6PHA:/mnt/d/code/rust_code$ curl https://sh.rustup.rs -sSf | 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. It will add the cargo, rustc, rustup and other commands to Cargo's bin directory, located at: /home/u/.cargo/bin This path will then be added to your PATH environment variable by modifying the profile file located at: /home/u/.profile You can uninstall at any time with rustup self uninstall and these changes will be reverted. Current installation options: default host triple: x86_64-unknown-linux-gnu default toolchain: stable modify PATH variable: yes 1) Proceed with installation (default) # 繼續安裝 (默認) 2) Customize installation # 自定義安裝 3) Cancel installation # 取消安裝 >1 info: syncing channel updates for 'stable-x86_64-unknown-linux-gnu' info: latest update on 2019-02-28, rust version 1.33.0 (2aa4c46cf 2019-02-28) info: downloading component 'rustc' 84.7 MiB / 84.7 MiB (100 %) 13.1 MiB/s ETA: 0 s info: downloading component 'rust-std' 56.8 MiB / 56.8 MiB (100 %) 13.5 MiB/s ETA: 0 s info: downloading component 'cargo' info: downloading component 'rust-docs' info: installing component 'rustc' 84.7 MiB / 84.7 MiB (100 %) 9.1 MiB/s ETA: 0 s info: installing component 'rust-std' 56.8 MiB / 56.8 MiB (100 %) 10.5 MiB/s ETA: 0 s info: installing component 'cargo' info: installing component 'rust-docs' 6.8 MiB / 8.5 MiB ( 80 %) 16.0 KiB/s ETA: 1.8390096028645833 min 50.3405761718 8.5 MiB / 8.5 MiB (100 %) 153.6 KiB/s ETA: 0 s info: default toolchain set to 'stable' stable installed - rustc 1.33.0 (2aa4c46cf 2019-02-28) Rust is installed now. Great! To get started you need Cargo's bin directory ($HOME/.cargo/bin) in your PATH environment variable. Next time you log in this will be done automatically. To configure your current shell run source $HOME/.cargo/env
安裝完成後,在$HOME/.cargo/bin文件夾下能夠看到以下的可執行程序(該目錄默認會自動添加環境變量).git
u@DESKTOP-JSC6PHA:~/.cargo/bin$ ls -al total 126528 drwxrwxrwx 1 u u 512 Apr 11 22:41 . drwxrwxrwx 1 u u 512 Apr 11 22:43 .. -rwxr-xr-x 12 u u 10794568 Apr 11 22:41 cargo # 包管理器 -rwxr-xr-x 12 u u 10794568 Apr 11 22:41 cargo-clippy # -rwxr-xr-x 12 u u 10794568 Apr 11 22:41 cargo-fmt # 源代碼格式化工具 -rwxr-xr-x 12 u u 10794568 Apr 11 22:41 cargo-miri -rwxr-xr-x 12 u u 10794568 Apr 11 22:41 clippy-driver -rwxr-xr-x 12 u u 10794568 Apr 11 22:41 rls # 爲編輯器準備的代碼提示工具 -rwxr-xr-x 12 u u 10794568 Apr 11 22:41 rust-gdb # 調試器 -rwxr-xr-x 12 u u 10794568 Apr 11 22:41 rust-lldb # 調試器 -rwxr-xr-x 12 u u 10794568 Apr 11 22:41 rustc # 編譯器 -rwxr-xr-x 12 u u 10794568 Apr 11 22:41 rustdoc # 文檔生成器 -rwxr-xr-x 12 u u 10794568 Apr 11 22:41 rustfmt # .rs文件格式化工具 -rwxr-xr-x 12 u u 10794568 Apr 11 22:41 rustup # 管理這套工具鏈下載更新的工具
檢查是否安裝成功,使用rustc -V
命令查看版本信息github
u@DESKTOP-JSC6PHA:~/.cargo$ rustc -V rustc 1.33.0 (2aa4c46cf 2019-02-28)
經常使用命令
rustup self update # 更新rustup rustup self uninstall # 卸載rust全部程序 rustup update # 更新工具鏈 rustup install nightly # 安裝nightly版本的編譯工具鏈 rustup default nightly # 設置默認工具鏈是nightly版本
Rust Language Server
RLS是官方提供的一個標準化的編輯器加強工具。 項目地址:https://github.com/rust-lang-nursery/rls
安裝方法shell
rustup self update rustup update nightly # 安裝RLS rustup component add rls --toolchain nightly rustup component add rust-analysis --toolchain nightly rustup component add rust-src --toolchain nightly
使用國內的鏡像加速訪問:
1. 加速rustup
export RUSTUP_DIST_SERVER=https://mirrors.ustc.edu.cn/rust-static export RUSTUP_UPDATE_ROOT=https://mirrors.ustc.edu.cn/rust-static/rustup
2. 加速cargo (cargo是一個包管理工具,相似於python的pip)
在$HOME/.cargo目錄下建立一個名爲config的文本文件,並添加以下內容windows
[source.crates-io] registry="https://github.com/rust-lang/crates.io-index" replace-with = "ustc" [source.ustc] registry="git://mirrors.ustc.edu.cn/crates.io-index"
Hello World
1. 新建hello_world.rs文件,用文本編輯器打開並輸入如下內容
// hello_world.rs fn main() { let s = "hello world! "; println!("{}",s); }
2. 編譯及運行
使用rustc hello_world.rs
命令,命令執行成功後,會生成一個hello_world的可執行程序,而後執行這個程序,效果以下 bash
3. hello_world代碼分析
- 通常Rust源代碼的後綴使用.rs表示 (源碼注意使用utf-8編碼)
- 單行註釋 //
- fn 是一個關鍵字,函數定義必須以 fn 開頭。函數體用大括號來包含。
- 默認狀況下,main函數是可執行程序的入口點,它是一個無參數、無返回值的函數
- 局部變量使用 let 開頭,雙引號包含起來的部分是字符串常量。
- 每條語句使用分號結尾
- 使用println!宏來進行簡單的標準輸出