1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264

//单行注释

///文档注释
/// # 示例
/// ```
/// let five = 5
/// ```

/////////////////////
// 1、基础
////////////////////

//此为无效代码
#[allow(dead_code)]
//函数
fn add2(x:i32, y:i32) -> i32 {
x+y
}

//main 函数
#[allow(unused_variables)]
#[allow(unused_assignments)]
#[allow(dead_code)]
fn main(){

//数据

//不可变绑定
let x:i32 = 1;

//整数和浮点数后缀
let y:i32 = 13i32;
let f:f64 = 1.3f64;

//类型推断
//大多数情况下rust编译器可以自行推断变量的类型,无需显示写出变量的类型
let int_x = 1;
let flot_y = 1.3;

//算术运算
let sum = x + y + 13;

//可变变量
let mut mutable = 1;
mutable = 2;
mutable += 3;

//字符串
let x: &str = "hello world!";

//打印输出
println!("{} {}", f, x); // output> 1.3 hello world!

//字符串
let s:String = "hello world".to_string();

//字符串切片
let s_slice:&str = &s;
println!("{} {}", s, s_slice); // output> hello world hello world

//容器、数组
//固定大小的数组
let four_ints:[i32;4] = [1,2,3,4];

//动态数组
let mut vector: Vec<i32> = vec![1,2,3,4];
vector.push(5);

//切片
let slice:&[i32] = &vector;

//使用 '{:?}' 打印debug样式
println!("{:?} {:?}", vector, slice); // output> [1, 2, 3, 4, 5] [1, 2, 3, 4, 5]

//元组,固定大小值的集合(可以有不同类型)
let x:(i32, &str, f64) = (1, "hello world", 3.4);

//解构
let (a,b,c) = x;
println!("{} {} {}", a, b, c); // output> 1 hello world 3.4

//下标访问元素
println!("{}", x.1); // output> hello world

//类型

//结构体
struct Point{
x:i32,
y:i32,
}

let origin:Point = Point{x:0, y:0};

//元组类型结构体
struct Point2(i32,i32);

let origin2 = Point2(0,0);

//枚举类型
//C语言样式的枚举类型
enum Direction {
Left,
Right,
Up,
Down,
}

let up = Direction::Up;

enum OptionalI32 {
AnI32(i32),
Nothing,
}

let two:OptionalI32 = OptionalI32::AnI32(2);
let nothing = OptionalI32::Nothing;

//泛型
struct Foo<T> {bar:T}

enum Optional<T> {
SomeVal(T),
NoVal,
}

//方法
impl<T> Foo<T> {

fn bar(&self) -> &T {
&self.bar
}

fn bar_mut(&mut self) -> &mut T{
&mut self.bar
}

fn into_bar(self) -> T {
self.bar
}
}

let a_foo = Foo{bar:1};
println!("{}", a_foo.bar()); //output> 1

//trait (/接口)
trait Frobnicate<T> {
fn frobnicate(self) -> Option<T>;
}

impl <T> Frobnicate<T> for Foo<T> {
fn frobnicate(self) -> Option<T> {
Some(self.bar)
}
}

let another_foo = Foo{bar:1};
println!("{:?}", another_foo.frobnicate()); //output> Some(1)

// 模式匹配
let foo = OptionalI32::AnI32(1);
match foo {
OptionalI32::AnI32(n) => println!("It's an i32 : {}", n),
OptionalI32::Nothing=> println!("It's nothing"),
} //output> It's an i32 : 1

//高级模式匹配
struct FooBar{x:i32, y:OptionalI32}
let bar = FooBar{x:15, y:OptionalI32::AnI32(32)};

match bar {
FooBar{x:0, y:OptionalI32::AnI32(0)} =>
println!("The numbers are zeor"),
FooBar{x:n, y:OptionalI32::AnI32(m)} if n==m =>
println!("The numbers are the same"),
FooBar{x:n, y:OptionalI32::AnI32(m)} =>
println!("Different numbers:{} {}",n,m),
FooBar{x:_, y:OptionalI32::Nothing} =>
println!("The second number is Nothing"),
}//output> Different numbers:15 32

//控制结构

//for循环
let array = [1,2,3];
for i in array.iter() {
println!("{}",i); //output> 1 2 3
}

//range,固定循环次数
for i in 0u32..10 {
print!("{}",i);
}
println!("");
//output> 0123456789

//if控制
if 1==1 {
println!("math working");
}else {
println!("oh, no")
}// output> math working

//if 做表达式
let value = if true {
"good"
} else {
"bad"
};

//while 循环
while 1 == 1 {
println!("while loop.");

break;
}

//死循环
loop {
println!("loop.");

break;
}

//内存安全和指针

let mut mine:Box<i32> = Box::new(3);
*mine = 5;

//转移所有权
let mut now_its_mine = mine;
*now_its_mine += 2;

println!("{}", now_its_mine);// output> 7
// println!("{}",mine);//将会报错,所有权已经转移

//引用
let mut var = 4;
var = 3;
let ref_var:&i32 = &var;
println!("{}", var); //output> 3
println!("{}", *ref_var);//output> 3

//可变引用
let mut var2 = 4;
let ref_var2:&mut i32 = &mut var2;
*ref_var2 += 2;
println!("{}", *ref_var2);//output> 6

//生命周期
struct View<'a> {
x: &'a i32,
}
impl <'a> View<'a> {
fn display(&self) {
println!("value x:{}", self.x);
}
}
let y = &90;
let b = View{x:y};
b.display(); //output> value x:90
}