今日主要围绕教程进行学习并记录有用的笔记
1. Prepare
前天已经将环境搭好,顺手找了一下在线的英文网和中文网:
The Rust Programming Language - The Rust Programming Language
1.1 Introduction
High-level ergonomics(效率) and low-level control are often at odds in programming language design; Rust challenges that conflict.
Rust also brings contemporary developer tools to the systems programming world:
- Cargo, the included dependency manager and build tool(内置的依赖管理器和构建工具), makes adding, compiling, and managing dependencies painless and consistent across the Rust ecosystem.
- The Rustfmt formatting tool ensures a consistent coding style across developers.
- The Rust Language Server powers Integrated Development Environment (IDE) integration for code completion and inline error messages.
1.2 Format
# install
rustup component add rustfmt
# fmt
cargo fmt
# build
cargo build
# fix
cargo fix
1.3 Lint
# install
rustup component add clippy
# lint
cargo clippy
2. Getting started
2.1 Installing Rust
# 安装
curl --proto '=https' --tlsv1.2 https://sh.rustup.rs -sSf | sh
# 更新
rustup update
# 卸载
rustup self uninstall
# 文档
rustup doc
2.2 Hello, world!
2.3 Cargo, Rust’s package manager and build system
- 扩展名为 .rs 的源文件在rustc编译后得到可执行文件(在 Windows 下是 main.exe,其它平台是 main),以及当使用 CMD 时会有一个包含调试信息、扩展名为 .pdb 的文件
Tips:
1. Rust 的缩进风格使用 4 个空格,而不是 1 个制表符(tab)
2. 如果文件名包含多个单词,那么按照命名习惯,应当使用下划线来分隔单词。例如命名为 hello_world.rs,而不是 helloworld.rs
3. 大部分 Rust 代码行以分号结尾4. Rust 中,代码包被称为 crates
2.4 Build & Release
cargo build --release