Skip to content

Commit bf604a6

Browse files
author
Naupio Z.Y. Huang
committed
add 04-02-primitive-type && update 04-01 (#173)
* fix #160 * rewrite 04 chapter: add 04-00-rust-travel * fix #163 * update 12-01 * add 04-01-primitive-type && update 04-00 * add 04-01-primitive-type && update 04-00 * add 04-01-primitive-type && update 04-00 * update readme * update readme
1 parent b7177ee commit bf604a6

File tree

3 files changed

+97
-38
lines changed

3 files changed

+97
-38
lines changed

04-quickstart-rewrite-WIP/04-00-rust-travel.md renamed to 04-quickstart-rewrite-WIP/04-01-rust-travel.md

Lines changed: 23 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,10 @@
88

99
- 创建一个 Doing 目录和 helloworld.rs 文件
1010

11-
> ps: mkdir ~/Doing
12-
13-
> ps: cd ~/Doing
14-
15-
> ps: notepad helloworld.rs # 作者偏向于使用 sublime 作为编辑器
16-
17-
> ps: subl helloworld.rs # 本章以后使用 subl 代替 notepad
11+
> ps: mkdir ~/Doing
12+
> ps: cd ~/Doing
13+
> ps: notepad helloworld.rs # 作者偏向于使用 sublime 作为编辑器
14+
> ps: subl helloworld.rs # 本章以后使用 subl 代替 notepad
1815
1916
注意这里用的后缀名是.rs,一般编程语言的代码文件都有惯用的后缀名,比如:
2017
C语言是.c,java是.java,python是.py等等,**请务必记住Rust语言的惯用后缀名是.rs**(虽然用别的后缀名也能通过rustc的编译)。
@@ -29,15 +26,13 @@ fn main() {
2926

3027
- 编译 helloworld.rs 文件
3128

32-
> ps: rustc helloworld.rs
33-
34-
> ps: rustc helloworld.rs -O # 也可以选择优化编译
29+
> ps: rustc helloworld.rs
30+
> ps: rustc helloworld.rs -O # 也可以选择优化编译
3531
3632
- 运行程序
3733

38-
> ps: ./helloworld.exe # windows 平台下需要加 .exe 后缀
39-
40-
> Hello World!
34+
> ps: ./helloworld.exe # windows 平台下需要加 .exe 后缀
35+
> Hello World!
4136
4237
没有`ps:`前缀的表示为控制台打印输出。
4338

@@ -54,27 +49,23 @@ fn main() {
5449

5550
- 创建项目 hellorust
5651

57-
> ps: cargo new hellorust --bin
52+
> ps: cargo new hellorust --bin
5853
5954
- 查看目录结构
6055

61-
> ps: tree # win10 powershell 自带有 tree 查看文件目录结构的功能
62-
63-
> └─hellorust
64-
65-
> ----└─src
56+
> ps: tree # win10 powershell 自带有 tree 查看文件目录结构的功能
57+
> └─hellorust
58+
> ----└─src
6659
6760
这里显示的目录结构,在hellorust目录下有 src 文件夹和 Cargo.toml 文件,同时这个目录会初始化为 git 项目
6861

6962
- 查看Cargo.toml文件
7063

71-
> ps: cat Cargo.toml
72-
73-
> [package]
74-
name = "hellorust"
75-
version = "0.1."
76-
authors = ["YourName <YourEmail>"]
77-
64+
> ps: cat Cargo.toml
65+
> [package]
66+
name = "hellorust"
67+
version = "0.1."
68+
authors = ["YourName <YourEmail>"]
7869
> [dependencies]
7970
8071
- 编辑src目录下的main.rs文件
@@ -99,14 +90,9 @@ fn main() {
9990

10091
- 编译和运行
10192

102-
> ps: cargo build
103-
104-
> ps: cargo build --release # 这个属于优化编译
105-
106-
> ps: ./target/debug/hellorust.exe
107-
108-
> ps: ./target/release/hellorust.exe # 如果前面是优化编译,则这样运行
109-
110-
> ps: cargo run # 编译和运行合在一起
111-
112-
> ps: cargo run --release # 同上,区别是是优化编译的
93+
> ps: cargo build
94+
> ps: cargo build --release # 这个属于优化编译
95+
> ps: ./target/debug/hellorust.exe
96+
> ps: ./target/release/hellorust.exe # 如果前面是优化编译,则这样运行
97+
> ps: cargo run # 编译和运行合在一起
98+
> ps: cargo run --release # 同上,区别是是优化编译的
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# 基本类型
2+
3+
## 变量绑定
4+
Rust 通过 let 关键字进行变量绑定。
5+
```rust
6+
fn main() {
7+
let a1 = 5;
8+
let a2:i32 = 5;
9+
assert_eq!(a1, a2);
10+
//let 绑定 整数变量默认类型推断是 i32
11+
12+
let b1:u32 = 5;
13+
//assert_eq!(a1, b1);
14+
//去掉上面的注释会报错,因为类型不匹配
15+
//errer: mismatched types
16+
}
17+
```
18+
这里的 assert_eq! 宏的作用是判断两个参数是不是相等的,但如果是两个不匹配的类型,就算字面值相等也会报错。
19+
20+
## 可变绑定
21+
rust 在声明变量时,在变量前面加入 mut 关键字,变量就会成为可变绑定的变量。
22+
```rust
23+
fn main() {
24+
let mut a: f64 = 1.0;
25+
let b = 2.0f32;
26+
27+
//改变 a 的绑定
28+
a = 2.0;
29+
println!("{:?}", a);
30+
31+
//重新绑定为不可变
32+
let a = a;
33+
34+
//不能赋值
35+
//a = 3.0;
36+
37+
//类型不匹配
38+
//assert_eq!(a, b);
39+
}
40+
```
41+
这里的 b 变量,绑定了 2.0f32。这是 Rust 里面值类型显式标记的语发。语法规定为`value`+`tpye`的形式。
42+
43+
**例如:**
44+
固定大小类型:
45+
> 1u8 1i8
46+
> 1u16 1i16
47+
> 1u32 1i32
48+
> 1u64 1i64
49+
50+
可变大小类型:
51+
> 1usize 1isize
52+
53+
浮点类型:
54+
> 1f32 1f64
55+
56+
## let解构
57+
为什么在 Rust 里面生命一个变量的时候要采用 let 绑定语法?
58+
那是因为 let 绑定语法的表达能力更强,而且 let 语言其实是一种模式。
59+
60+
**例如:**
61+
```rust
62+
fn main() {
63+
let (a, mut b): (bool,bool) = (true, false);
64+
println!("a = {:?}, b = {:?}", a, b);
65+
//a 不可变绑定
66+
//a = false;
67+
68+
//b 可变绑定
69+
b = true;
70+
assert_eq!(a, b);
71+
}
72+
```
73+
这里使用了 bool 类型,bool 类型只有 true 和 false 两个值。通常用来做逻辑判断。

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ https://wayslog.gitbooks.io/rustprimer/content/
3232
4. [Rust一小时快速入门](./04-quickstart/04-00-intro.md)「ee0703」
3333
1. [第一个Rust程序](./04-quickstart/04-01-hello-world.md)
3434
2. [简单的数学运算](./04-quickstart/04-02-basic-math.md)
35-
3. [快速上手](./04-quickstart/04-03-cheet-sheet.md)
35+
3. [快速上手](./04-quickstart/04-03-cheet-sheet.md)
3636
5. [Cargo项目管理器](./05-cargo-projects-manager/05-cargo-projects-manager.md)「fuyingfuying」
3737
6. [基本程序结构](./06-flow/06-00-preface.md)「daogangtang」
3838
1. [注释](./06-flow/06-01-comment.md)

0 commit comments

Comments
 (0)