Rust Playground
如果只是寫Demo測試,嘗試下 Rust,可以無需安裝Rust開發環境,直接在Rust Playground運行自己代碼。
開發環境安裝
強烈不建議各位童鞋使用Windows學習和開發Rust,如果預裝了Win10系統,可以在Win10的Linux子系統進行配置,啓用Win10的Linux子系統的教程見鏈接。
如果你的主機不是windows系統,Mac OS, linux,Win10下Linux子系統或其他類Unix系統的話,都可以用
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
這條命令來安裝最新的Rust穩定版。
安裝完畢後,在終端輸入rustc --version
即可查看當前的Rust版本:
root@host:~# rustc --version
rustc 1.38.0 (625451e37 2019-09-23)
crates.io換國內中科大/阿里雲鏡像源
Rust社區公開的第三方包都集中在crates.io網站上面,他們的文檔被自動發佈到doc.rs
網站上。Rust提供了非常方便的包管理器cargo,它類似於Node.js
的npm
和Python
的pip
。但cargo不僅侷限於包管理,還爲Rust生態系統提供了標準的工作流。
在實際開發中,爲了更快速下載第三方包,我們需要把crates.io換國內的鏡像源。
換國內中科大源
執行以下命令即可:
tee $HOME/.cargo/config <<-'EOF'
[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"
EOF
換阿里雲源
tee $HOME/.cargo/config <<-'EOF'
[source.crates-io]
replace-with = "rustcc"
[source.rustcc]
registry = "https://code.aliyun.com/rustcc/crates.io-index"
EOF
hello world程序
新建一個名爲hello.rs的文件,寫入以下內容:
fn main()
{
println!("hello world!");
}
然後執行命令rustc hello.rs
進行編譯,會生成一個和代碼文件同名的可執行文件hello
,運行:
./hello
就能看到終端打印出hello world!
如果想指定編譯後生成的可執行文件名,只需加參數-o
,比如:
rustc hello.rs -o hello_test
會編譯hello.rs
並生成名爲hello_test
的可執行文件。