mirror of
https://github.com/gnu4cn/rust-lang-zh_CN.git
synced 2025-03-14 19:30:29 +08:00
Refining Ch03.
This commit is contained in:
parent
1e2d49aeb6
commit
9e01554d49
8
projects/data_types/Cargo.toml
Normal file
8
projects/data_types/Cargo.toml
Normal file
@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "data_types"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
3
projects/data_types/src/main.rs
Normal file
3
projects/data_types/src/main.rs
Normal file
@ -0,0 +1,3 @@
|
||||
fn main() {
|
||||
let guess = "42".parse().expect("这不是个数字!");
|
||||
}
|
@ -1,36 +1,47 @@
|
||||
# 数据类型
|
||||
|
||||
Rust 的所有值,都属于某种确切的 *数据类型(data type)*,数据类型告诉 Rust 所指定的是何种数据,进而 Rust 才知道该怎样使用那个数据。接下来会看看两个数据类型的子集:标量(scalar)类型与复合(compound)类型。
|
||||
**Data Types**
|
||||
|
||||
|
||||
Rust 中的每个值,都属于特定的 *数据类型,data type*,这会告诉 Rust,正被指定的是何种数据类型,以便他知道,如何处理这个数据。我们将研究两个数据类型的子集:标量类型和复合类型,scalar and compound。
|
||||
|
||||
请记住,Rust 是门 *静态类型,statically typed* 的语言,这意味着在编译时,他必须知道所有变量的类型。编译器通常可以根据值,以及咱们使用值的方式,推断出我们打算使用的类型。在可能存在多种类型的情况下,例如在第 2 章 [“将猜数与秘密数字进行比较”](../Ch02_Programming_a_Guessing_Game.md#将猜数与秘数相比较) 小节中,咱们曾使用 `parse` 将一个 `String` 转换为数字类型时,我们就必须添加一个类型注解,就像下面这样:
|
||||
|
||||
请牢记 Rust 是门 *静态类型(statically typed)* 语言,这就意味着在运行时,他必须清楚所有变量的类型。基于值与对变量的使用方式,编译器通常可以推断出希望变量使用何种类型来。在可能有许多中类型的情况下,就如同第 2 章 [将猜数与秘密数字比较](Ch02_Programming_a_Guessing_Game.md#将猜数与秘数相比较) 小节中,使用 `parse` 把一个 `String` 转换成数字类型时,就必须添加一个类型注释,如下面这样:
|
||||
|
||||
```rust
|
||||
let guess: u32 = "42".parse().expect("这不是个数字!");
|
||||
```
|
||||
|
||||
若这里添加类型注解,那么 Rust 就会给出下面的错误,表示编译器需要更多信息来明白这里想要使用何种类型:
|
||||
|
||||
如果我们不添加上面代码中所示的 `: u32` 类型注解,Rust 将显示如下错误,这意味着编译器需要我们提供更多信息,才能知道我们打算使用哪种类型:
|
||||
|
||||
|
||||
```console
|
||||
$ cargo build 101 ✘
|
||||
Compiling variables v0.1.0 (/home/peng/rust-lang/projects/variables)
|
||||
$ cargo build
|
||||
Compiling data_types v0.1.0 (C:\tools\msys64\home\Lenny.Peng\rust-lang-zh_CN\projects\data_types)
|
||||
error[E0282]: type annotations needed
|
||||
--> src/main.rs:19:9
|
||||
|
|
||||
19 | let guess = "42".parse().expect("那不是个数字!");
|
||||
| ^^^^^ consider giving `guess` a type
|
||||
--> src\main.rs:2:9
|
||||
|
|
||||
2 | let guess = "42".parse().expect("这不是个数字!");
|
||||
| ^^^^^
|
||||
|
|
||||
help: consider giving `guess` an explicit type
|
||||
|
|
||||
2 | let guess: /* Type */ = "42".parse().expect("这不是个数字!");
|
||||
| ++++++++++++
|
||||
|
||||
For more information about this error, try `rustc --explain E0282`.
|
||||
static HELLO_WORLD: &str = "你好,世界!";
|
||||
|
||||
fn main() {
|
||||
println! ("名字为:{}", HELLO_WORLD);
|
||||
}rror: could not compile `variables` due to previous error
|
||||
error: could not compile `data_types` (bin "data_types") due to previous error
|
||||
```
|
||||
|
||||
接下来就会见识到其他数据类型的类型注解。
|
||||
|
||||
咱们将看到,其他数据类型的不同类型注解。
|
||||
|
||||
|
||||
## 标量类型(Scalar Types)
|
||||
## 标量类型
|
||||
|
||||
**Scalar Types**
|
||||
|
||||
|
||||
*标量* 类型,表示单个值。Rust 有着四个主要的标量类型:整数、浮点数、布尔值与字符。这些类型,其他语言也有。下面就深入看看他们在 Rust 中是怎样工作的。
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user