mirror of
https://github.com/rust-lang-cn/book-cn.git
synced 2025-01-23 07:20:20 +08:00
Update types chapters as per @aturon's feedback
This commit is contained in:
parent
ff1b2b8a13
commit
f6534b1754
@ -5,14 +5,30 @@ These types can group multiple values of scalar types into another type.
|
||||
|
||||
## Tuples
|
||||
|
||||
We’ve previously seen our first compound type: tupes. Tuples have an ‘arity’,
|
||||
or size. We might say “that’s a 3-tuple” or “that’s a 5-tuple.”
|
||||
We’ve seen tuples before, in the guise of binding or returning multiple values
|
||||
at once. It turns out that there’s no magic here: tuples are a general way of
|
||||
making a compound value that groups some number of other values with distinct
|
||||
types. The number of values grouped is the ‘arity’ of the tuple.
|
||||
|
||||
Each position in a tuple has a distinct type:
|
||||
We create a tuple by writing a comma-separated list of values inside
|
||||
parentheses; each position in the tuple has a distinct type:
|
||||
|
||||
```rust
|
||||
fn main() {
|
||||
let x: (i32, f64, u8) = (500, 6.4, 1);
|
||||
let tup: (i32, f64, u8) = (500, 6.4, 1);
|
||||
}
|
||||
```
|
||||
|
||||
Note that, unlike the examples of multiple bindings, here we bound the
|
||||
single name `tup` to the entire tuple. We can then use pattern
|
||||
matching to destructure this tuple value:
|
||||
|
||||
```rust
|
||||
fn main() {
|
||||
let tup: (i32, f64, u8) = (500, 6.4, 1);
|
||||
let (x, y, z) = tup;
|
||||
|
||||
println!("The value of y is: {}", y);
|
||||
}
|
||||
```
|
||||
|
||||
@ -21,8 +37,8 @@ are anonymous, which can make code hard to read.
|
||||
|
||||
### Tuple indexing
|
||||
|
||||
To access an element of a tuple, we use a `.` followed by the index we want to
|
||||
access:
|
||||
In addition to destructuring through pattern matching, we can also access a
|
||||
tuple element directly using `.`, followed by the index we want to access:
|
||||
|
||||
```rust
|
||||
fn main() {
|
||||
@ -143,18 +159,8 @@ It says that our thread panicked, and that our program didn’t exit
|
||||
successfully. There’s also a reason: we had a length of five, but an index of
|
||||
10.
|
||||
|
||||
A ‘panic’ can also be induced manually, with the `panic!` macro:
|
||||
|
||||
```rust,should_panic
|
||||
fn main() {
|
||||
panic!("Oh no!");
|
||||
}
|
||||
```
|
||||
|
||||
When the `panic!` macro runs, it will cause a panic. When a Rust program
|
||||
panics, it starts a kind of controlled crash. The current thread of execution
|
||||
will stop entirely. As such, panics are reserved for serious, program-ending
|
||||
errors. They’re not a general error-handling mechanism.
|
||||
For now, all you need to know is that a panic will crash your program. Rust’s
|
||||
error handling story is described in full in a later chapter.
|
||||
|
||||
So why did this code panic? Well, arrays know how many elements they hold. When
|
||||
we access an element via indexing, Rust will check that the index is less than
|
||||
@ -163,8 +169,6 @@ our first example of Rust’s safety principles in action. In many low-level
|
||||
languages, this kind of check is not done. If you have an incorrect index,
|
||||
invalid memory can be accessed. Rust protects us against this kind of error.
|
||||
|
||||
**Steve’s note: this next bit might be our first ‘advanced’ section, on get()?**
|
||||
|
||||
### Debug
|
||||
|
||||
So far, we’ve been printing values using `{}`. If we try that with an array,
|
||||
@ -198,14 +202,15 @@ error: aborting due to previous error
|
||||
Whew! The core of the error is this part: the trait `core::fmt::Display` is not
|
||||
implemented. We haven’t discussed traits yet, so this is bound to be confusing!
|
||||
Here’s all we need to know for now: `println!` can do many kinds of formatting.
|
||||
By default, `{}` implements a kind of formatting known as `Display`: output for
|
||||
end-users. The primitive types we’ve seen so far implement `Display`, as
|
||||
there’s only one way you’d show a `1` to a user. But with arrays, the output is
|
||||
less clear. Do you want commas or not? What about the `[]`s?
|
||||
By default, `{}` implements a kind of formatting known as `Display`: output
|
||||
intended for direct end-user consumption. The primitive types we’ve seen so far
|
||||
implement `Display`, as there’s only one way you’d show a `1` to a user. But
|
||||
with arrays, the output is less clear. Do you want commas or not? What about
|
||||
the `[]`s?
|
||||
|
||||
Due to these questions, more complex types in the standard library do not
|
||||
implement `Display` formatting. There is another kind of formatting, `Debug`,
|
||||
which is a bit different: output for programmers and debuggers. We can ask
|
||||
which is a bit different: intended for programmer consumption. We can ask
|
||||
`println!` to use `Debug` formatting with `:?`:
|
||||
|
||||
```rust
|
||||
|
@ -29,9 +29,9 @@ Here’s a chart of Rust’s integer types:
|
||||
| arch | isize | usize |
|
||||
|
||||
We have both signed and unsigned variants of numbers, and each variant has an
|
||||
explicit size. Unsigned numbers are always positive, and signed numbers can be
|
||||
explicit size. Unsigned numbers are never negative, and signed numbers can be
|
||||
positive or negative. (Think ‘plus sign’ or ‘minus sign’: that’s a signed
|
||||
number.) Signed numbers are stored using ‘two’s compliment’ representation.
|
||||
number.) Signed numbers are stored using ‘two’s complement’ representation.
|
||||
|
||||
Finally, `isize` and `usize` are different sizes based on the kind of computer
|
||||
your program is running on. If you are on a 64-bit architecture, they are 64
|
||||
@ -58,6 +58,29 @@ fn main() {
|
||||
Floating-point numbers are represented according to the IEEE-754 standard.
|
||||
`f32` is a single-precision float, `f64` is double-precision.
|
||||
|
||||
## Numeric operations
|
||||
|
||||
Rust supports the usual operations you’d expect on all of these number types:
|
||||
|
||||
```rust
|
||||
fn main() {
|
||||
// addition
|
||||
let sum = 5 + 10;
|
||||
|
||||
// subtraction
|
||||
let difference = 95.5 - 4.3;
|
||||
|
||||
// multiplication
|
||||
let product = 4 * 30;
|
||||
|
||||
// division
|
||||
let quotient = 56.7 / 32.2;
|
||||
|
||||
// modulus
|
||||
let remainder = 43 % 5;
|
||||
}
|
||||
```
|
||||
|
||||
## Booleans
|
||||
|
||||
Somewhat fundamental to all computing, Rust has a boolean type, `bool`, with
|
||||
@ -70,7 +93,8 @@ fn main() {
|
||||
}
|
||||
```
|
||||
|
||||
That’s really all there is to say about that!
|
||||
The main way to consume boolean values is through conditionals like `if`, which
|
||||
we’ll see later in the chapter.
|
||||
|
||||
## Characters
|
||||
|
||||
@ -79,18 +103,14 @@ primitive alphabetic type is the `char`:
|
||||
|
||||
```rust
|
||||
fn main() {
|
||||
let c = 'z';
|
||||
let z = 'ℤ';
|
||||
let c = 'z';
|
||||
let z = 'ℤ';
|
||||
}
|
||||
```
|
||||
|
||||
Rust’s `char` represents a [Unicode Scalar Value], which means that it can
|
||||
represent a lot more than just ASCII. “Character” isn’t really a concept in
|
||||
represent a lot more than just ASCII. ‘Character’ isn’t really a concept in
|
||||
Unicode, however: your human intutition for what a ‘character’ is may not match
|
||||
up with a `char`. It also means that `char`s are four bytes each.
|
||||
|
||||
[Unicode Scalar Value]: http://www.unicode.org/glossary/#unicode_scalar_value
|
||||
|
||||
The single quotes are important: to define a literal single character, we use
|
||||
single quotes. If we used double quotes, we’d be defining a `&str`. Let’s talk
|
||||
about that next!
|
||||
|
Loading…
Reference in New Issue
Block a user