Rust 简介
Rust 语言是一种高效、可靠的通用高级语言。其高效不仅限于开发效率,它的执行效率也是令人称赞的,是一种少有的兼顾开发效率和执行效率的语言。
Rust 语言由 Mozilla 开发,最早发布于 2014 年 9 月。Rust 的编译器是在 MIT License 和 Apache License 2.0 双重协议声明下的免费开源软件。截至目前( 2020 年 1 月)最新的编译器版本是 1.41.0。
Rust 官方在线工具: https://play.rust-lang.org/。
为什么选择 Rust?
高性能
Rust 速度惊人且内存利用率极高。由于没有运行时和垃圾回收,它能够胜任对性能要求特别高的服务,可以在嵌入式设备上运行,还能轻松和其他语言集成。
可靠性
Rust 丰富的类型系统和所有权模型保证了内存安全和线程安全,让您在编译期就能够消除各种各样的错误。
生产力
Rust 拥有出色的文档、友好的编译器和清晰的错误提示信息, 还集成了一流的工具——包管理器和构建工具, 智能地自动补全和类型检验的多编辑器支持, 以及自动格式化代码等等。
安装
$ 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.
...
安装检查:
$ rustc -V
rustc 1.21.0 (3b72af97e 2017-10-09)
$ cargo -V
cargo 0.22.0 (3423351a5 2017-10-06)
更新版本:
$ rustup update
info: syncing channel updates for 'stable-x86_64-apple-darwin'
info: latest update on 2020-08-27, rust version 1.46.0 (04488afe3 2020-08-24)
info: downloading component 'rustc'
搭建开发环境
IDEA plugin
Rust language support:
https://plugins.jetbrains.com/plugin/8182-rust
在VS Code中开发
在线图书
https://doc.rust-lang.org/book/
Rust 实例学习
https://rustbyexample.com/hello.html
Introduction
- Hello World
fn main(){
println!("Hello World!");
}
build and
1.1. Comments
1.2. Formatted print
1.2.1. Debug
1.2.2. Display
1.2.2.1. Testcase: List
1.2.3. Formatting
- Primitives
2.1. Literals and operators
2.2. Tuples
2.3. Arrays and Slices - Custom Types
3.1. Structures
3.2. Enums
3.2.1. use
3.2.2. C-like
3.2.3. Testcase: linked-list
3.3. constants - Variable Bindings
4.1. Mutability
4.2. Scope and Shadowing
4.3. Declare first - Types
5.1. Casting
5.2. Literals
5.3. Inference
5.4. Aliasing - Conversion
6.1. From and Into
6.2. To and From String - Expressions
- Flow Control
8.1. if/else
8.2. loop
8.2.1. Nesting and labels
8.2.2. Returning from loops
8.3. while
8.4. for and range
8.5. match
8.5.1. Destructuring
8.5.1.1. tuples
8.5.1.2. enums
8.5.1.3. pointers/ref
8.5.1.4. structs
8.5.2. Guards
8.5.3. Binding
8.6. if let
8.7. while let - Functions
9.1. Methods
9.2. Closures
9.2.1. Capturing
9.2.2. As input parameters
9.2.3. Type anonymity
9.2.4. Input functions
9.2.5. As output parameters
9.2.6. Examples in std
9.2.6.1. Iterator::any
9.2.6.2. Iterator::find
9.3. Higher Order Functions - Modules
10.1. Visibility
10.2. Struct visibility
10.3. The use declaration
10.4. super and self
10.5. File hierarchy - Crates
11.1. Library
11.2. extern crate - Attributes
12.1. dead_code
12.2. Crates
12.3. cfg
12.3.1. Custom - Generics
13.1. Functions
13.2. Implementation
13.3. Traits
13.4. Bounds
13.4.1. Testcase: empty bounds
13.5. Multiple bounds
13.6. Where clauses
13.7. New Type Idiom
13.8. Associated items
13.8.1. The Problem
13.8.2. Associated types
13.9. Phantom type parameters
13.9.1. Testcase: unit clarification - Scoping rules
14.1. RAII
14.2. Ownership and moves
14.2.1. Mutability
14.3. Borrowing
14.3.1. Mutability
14.3.2. Freezing
14.3.3. Aliasing
14.3.4. The ref pattern
14.4. Lifetimes
14.4.1. Explicit annotation
14.4.2. Functions
14.4.3. Methods
14.4.4. Structs
14.4.5. Bounds
14.4.6. Coercion
14.4.7. static
14.4.8. elision - Traits
15.1. Derive
15.2. Operator Overloading
15.3. Drop
15.4. Iterators
15.5. Clone - macro_rules!
16.1. Syntax
16.1.1. Designators
16.1.2. Overload
16.1.3. Repeat
16.2. DRY (Don't Repeat Yourself)
16.3. DSL (Domain Specific Languages)
16.4. Variadics - Error handling
17.1. panic
17.2. Option & unwrap
17.2.1. Combinators: map
17.2.2. Combinators: and_then
17.3. Result
17.3.1. map for Result
17.3.2. aliases for Result
17.3.3. Early returns
17.3.4. Introducing ?
17.4. Multiple error types
17.4.1. Pulling Results out of Options
17.4.2. Defining an error type
17.4.3. Boxing errors
17.4.4. Other uses of ?
17.4.5. Wrapping errors
17.5. Iterating over Results - Std library types
18.1. Box, stack and heap
18.2. Vectors
18.3. Strings
18.4. Option
18.5. Result
18.5.1. ?
18.6. panic!
18.7. HashMap
18.7.1. Alternate/custom key types
18.7.2. HashSet - Std misc
19.1. Threads
19.1.1. Testcase: map-reduce
19.2. Channels
19.3. Path
19.4. File I/O
19.4.1. open
19.4.2. create
19.5. Child processes
19.5.1. Pipes
19.5.2. Wait
19.6. Filesystem Operations
19.7. Program arguments
19.7.1. Argument parsing
19.8. Foreign Function Interface - Meta
20.1. Documentation
20.2. Testing - Unsafe Operations
参考资料
https://www.rust-lang.org/zh-CN/tools
https://www.runoob.com/rust/rust-tutorial.html