Skip to content

Commit 285ef8b

Browse files
author
Naupio Z.Y. Huang
committed
Rewrite: 04-chapter (WIP,别合并) (#161)
* fix #160 * rewrite 04 chapter: add 04-00-rust-travel * fix #163
1 parent d78da24 commit 285ef8b

2 files changed

Lines changed: 114 additions & 1 deletion

File tree

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# Rust旅程
2+
3+
## HelloWorld
4+
按照编程语言的传统,学习第一门编程语言的第一个程序都是打印 Hello World!
5+
下面根据我们的步骤创建 Rust 的 Hello World!程序:
6+
7+
**下面的命令操作,如果没有特别说明,都是在shell下运行。本文为了简单统一,所有例子都在 win10 的 powershell 下运行,所有命令都运行在`ps:`标识符之后**
8+
9+
- 创建一个 Doing 目录和 helloworld.rs 文件
10+
11+
> ps: mkdir ~/Doing
12+
13+
> ps: cd ~/Doing
14+
15+
> ps: notepad helloworld.rs # 作者偏向于使用 sublime 作为编辑器
16+
17+
> ps: subl helloworld.rs # 本章以后使用 subl 代替 notepad
18+
19+
注意这里用的后缀名是.rs,一般编程语言的代码文件都有惯用的后缀名,比如:
20+
C语言是.c,java是.java,python是.py等等,**请务必记住Rust语言的惯用后缀名是.rs**(虽然用别的后缀名也能通过rustc的编译)。
21+
22+
- 在 helloworld.rs 文件中输入 Rust 代码
23+
24+
```rust
25+
fn main() {
26+
println!("Hello World!");
27+
}
28+
```
29+
30+
- 编译 helloworld.rs 文件
31+
32+
> ps: rustc helloworld.rs
33+
34+
> ps: rustc helloworld.rs -O # 也可以选择优化编译
35+
36+
- 运行程序
37+
38+
> ps: ./helloworld.exe # windows 平台下需要加 .exe 后缀
39+
40+
> Hello World!
41+
42+
没有`ps:`前缀的表示为控制台打印输出。
43+
44+
我们已经用rust编写第一个可执行程序,打印出了'hello world!',很酷,对吧!
45+
但是这段代码到底是什么意思呢,作为新手的你一定云里雾里吧,让我们先看一下这个程序:
46+
47+
1. 第一行中 fn 表示定义一个**函数**,main是这个函数的名字,花括号{}里的语句则表示这个函数的内容。
48+
2. 名字叫做**main**的函数有特殊的用途,那就是作为程序的入口,也就是说程序每次都从这个函数开始运行。
49+
3. 函数中只有一句 ```println!("Hello World!");```,这里```println!```是一个Rust语言自带的****
50+
这个宏的功能就是打印文本(结尾会换行),而"Hello World!"这个用引号包起来的东西是一个**字符串**,就是我们要打印的文本。
51+
4. 你一定注意到了```;```吧, 在Rust语言中,分号```;```用来把语句分隔开,也就是说语句的末尾一般用分号做为结束标志。
52+
53+
## HelloRust
54+
55+
- 创建项目 hellorust
56+
57+
> ps: cargo new hellorust --bin
58+
59+
- 查看目录结构
60+
61+
> ps: tree # win10 powershell 自带有 tree 查看文件目录结构的功能
62+
63+
> └─hellorust
64+
65+
> ----└─src
66+
67+
这里显示的目录结构,在hellorust目录下有 src 文件夹和 Cargo.toml 文件,同时这个目录会初始化为 git 项目
68+
69+
- 查看Cargo.toml文件
70+
71+
> ps: cat Cargo.toml
72+
73+
> [package]
74+
name = "hellorust"
75+
version = "0.1."
76+
authors = ["YourName <YourEmail>"]
77+
78+
> [dependencies]
79+
80+
- 编辑src目录下的main.rs文件
81+
82+
> ps: subl ./src/main.rs
83+
84+
cargo 创建的项目,在src目录下会有一个初始化的main.rs文件,内容为:
85+
```rust
86+
fn main() {
87+
println!("Hello, world!");
88+
}
89+
```
90+
现在我们编辑这个文件,改为:
91+
```rust
92+
fn main() {
93+
let rust = "Rust";
94+
println!("Hello, {}!", rust);
95+
}
96+
```
97+
这里的 `let rust = "Rust"` 是把 rust 变量绑定为 "Rust" ,
98+
`println!("Hello, {}!", rust);`里把 rust 变量的值代入到`"Hello, {}!"`中的`{}`
99+
100+
- 编译和运行
101+
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 # 同上,区别是是优化编译的

12-ownership-system/12-01-ownership.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,8 @@ fn main() {
261261
}
262262
```
263263
>结果: x=100, y=102
264-
264+
265+
注意: 例子1是比较特别的,使不使用 move 对结果都没什么影响,因为环境变量 x 是 i32 类型,属于 primitive type,实现了 Copy trait,在闭包使用 move 的时候,是先 clone 了一份 x ,在 move 的时候是 move 了这份 clone 的 x,所以后面的 println!引用 x 的时候没有报错。
265266
**例子2:**
266267
```rust
267268
fn main() {

0 commit comments

Comments
 (0)