Update types chapters as per @aturon's feedback

This commit is contained in:
Steve Klabnik 2015-12-29 17:33:46 -05:00
parent ff1b2b8a13
commit f6534b1754
2 changed files with 60 additions and 35 deletions

View File

@ -5,14 +5,30 @@ These types can group multiple values of scalar types into another type.
## Tuples
Weve previously seen our first compound type: tupes. Tuples have an arity,
or size. We might say “thats a 3-tuple” or “thats a 5-tuple.”
Weve seen tuples before, in the guise of binding or returning multiple values
at once. It turns out that theres 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 didnt exit
successfully. Theres 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. Theyre not a general error-handling mechanism.
For now, all you need to know is that a panic will crash your program. Rusts
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 Rusts 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.
**Steves note: this next bit might be our first advanced section, on get()?**
### Debug
So far, weve 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 havent discussed traits yet, so this is bound to be confusing!
Heres 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 weve seen so far implement `Display`, as
theres only one way youd 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 weve seen so far
implement `Display`, as theres only one way youd 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

View File

@ -29,9 +29,9 @@ Heres a chart of Rusts 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: thats a signed
number.) Signed numbers are stored using twos compliment representation.
number.) Signed numbers are stored using twos 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 youd 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() {
}
```
Thats really all there is to say about that!
The main way to consume boolean values is through conditionals like `if`, which
well 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 = '';
}
```
Rusts `char` represents a [Unicode Scalar Value], which means that it can
represent a lot more than just ASCII. “Character” isnt really a concept in
represent a lot more than just ASCII. Character isnt 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, wed be defining a `&str`. Lets talk
about that next!