Skip to content

Commit d7ab8cc

Browse files
authored
Update rust-intro.md
1 parent 8869747 commit d7ab8cc

1 file changed

Lines changed: 27 additions & 28 deletions

File tree

project/rust-intro.md

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -57,46 +57,45 @@ b = 1;
5757
5858
常见的数据类型有如下几类,很多时候我们不需要显示地指定数据类型,因为 Rust 编译器会帮我们自动推导类型,但是 `mut` 依旧需要标注。
5959

60-
- `i8`, `i16`, `i32`, `i64`, `i128`, `isize`:有符号整数,`isize` 表示大小取决于系统
60+
`i8`, `i16`, `i32`, `i64`, `i128`, `isize`:有符号整数,`isize` 表示大小取决于系统
6161

62-
- `u8`, ..., `usize`:无符号整数
62+
`u8`, ..., `usize`:无符号整数
6363

64-
- `f32`, `f64`:浮点数
64+
`f32`, `f64`:浮点数
6565

66-
- `char`**utf-8 字符**,实际可能占多个字节
66+
`char`**utf-8 字符**,实际可能占多个字节
6767

68-
- `(T1, ... )`:元组,使用`.0``.1`来访问元素
68+
`(T1, ... )`:元组,使用`.0``.1`来访问元素
6969

70-
```rust
71-
let mut tuple: (i32, i32, i32) = (1, 2, 3);
72-
tuple.0 = 4;
73-
```
70+
```rust
71+
let mut tuple: (i32, i32, i32) = (1, 2, 3);
72+
tuple.0 = 4;
73+
```
7474

75-
- `[T; len]`:数组,使用下标访问,超出数组长度会报错(编译期/运行期)
75+
`[T; len]`:数组,使用下标访问,超出数组长度会报错(编译期/运行期)
7676

77-
```rust
78-
let mut array: [i32; 3] = [1, 2, 3];
79-
array[3] = 4; //error, out of length!
80-
```
77+
```rust
78+
let mut array: [i32; 3] = [1, 2, 3];
79+
array[3] = 4; //error, out of length!
80+
```
8181

82-
- `Vec<T>`:变长数组,使用下标[...]访问,运行时进行边界检查。文档:[Vec in std::vec - Rust](https://doc.rust-lang.org/std/vec/struct.Vec.html)
82+
`Vec<T>`:变长数组,使用下标[...]访问,运行时进行边界检查。文档:[Vec in std::vec - Rust](https://doc.rust-lang.org/std/vec/struct.Vec.html)
8383

84-
```rust
85-
let mut a = Vec::new();
86-
a.push(1);
87-
a.push(2); //这里自动推导出a的类型是Vec<i32>
84+
```rust
85+
let mut a = Vec::new();
86+
a.push(1);
87+
a.push(2); //这里自动推导出a的类型是Vec<i32>
8888

89-
let mut b = vec![1, 2]; //这里自动推导出b的类型是Vec<i32>
90-
println!("{}", b[0]);
91-
```
89+
let mut b = vec![1, 2]; //这里自动推导出b的类型是Vec<i32>
90+
println!("{}", b[0]);
91+
```
9292

93-
- `String`:utf-8字符串。文档:[String in std::string - Rust](https://doc.rust-lang.org/std/string/struct.String.html)
94-
95-
```rust
96-
let mut a = String::from("这是一个字符串!");
93+
`String`:utf-8字符串。文档:[String in std::string - Rust](https://doc.rust-lang.org/std/string/struct.String.html)
9794

98-
a.push_str("这是添加到末尾的部分!");
99-
```
95+
```rust
96+
let mut a = String::from("这是一个字符串!");
97+
a.push_str("这是添加到末尾的部分!");
98+
```
10099

101100
Rust 中使用 `fn` 关键字声明函数,参数列表的声明方式和变量相同,使用 `->` 标记指定函数的返回值类型。
102101

0 commit comments

Comments
 (0)