mirror of
https://github.com/rust-lang/book.git
synced 2026-11-30 19:24:32 -06:00
Split up primitive types into scalar and compound types
In the process, drop &str and fn.
This commit is contained in:
+2
-1
@@ -10,7 +10,8 @@
|
||||
- [Primitive Rust](primitive-rust.md)
|
||||
- [Variable Bindings](variable-bindings.md)
|
||||
- [Functions](functions.md)
|
||||
- [Primitive Types](primitive-types.md)
|
||||
- [Scalar Types](scalar-types.md)
|
||||
- [Compound Types](compound-types.md)
|
||||
- [Comments](comments.md)
|
||||
- [`if`](if.md)
|
||||
- [Loops](loops.md)
|
||||
|
||||
@@ -1,67 +1,11 @@
|
||||
# Primitive Types
|
||||
# Compound Types
|
||||
|
||||
We’ve seen that every value in Rust has a type of some kind. There are a number
|
||||
of types which are built into the language itself. We call these types
|
||||
‘primitive’ types, since you can’t re-create them yourself. There are, of
|
||||
course, many non-primitive types provided by the standard library as well.
|
||||
|
||||
Remember, you can rely on type inference to figure out the type of a binding,
|
||||
or you can annotate it explicitly:
|
||||
|
||||
```rust
|
||||
fn main() {
|
||||
let x: i32 = 5;
|
||||
}
|
||||
```
|
||||
|
||||
## Integers
|
||||
|
||||
You’ve already seen one primitive type: `i32`. There are a number of built-in
|
||||
number types in Rust.
|
||||
|
||||
Here’s a chart of Rust’s integer types:
|
||||
|
||||
| | signed | unsigned |
|
||||
|--------|--------|----------|
|
||||
| 8-bit | i8 | u8 |
|
||||
| 16-bit | i16 | u16 |
|
||||
| 32-bit | i32 | u32 |
|
||||
| 64-bit | i64 | u64 |
|
||||
| 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
|
||||
positive or negative. (Think ‘plus sign’ or ‘minus sign’: that’s a signed
|
||||
number.) Signed numbers are stored using ‘two’s compliment’ 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
|
||||
bits, and if you’re on a 32-bit one, they’re 32 bits.
|
||||
|
||||
So how do you choose from all these options? Well, if you really don’t know,
|
||||
the defaults are a good choice: integer types default to `i32`. The primary use
|
||||
case for `isize`/`usize` is when indexing some sort of collection. We’ll talk
|
||||
more about our first collection, arrays, in just a moment.
|
||||
|
||||
## Floating-point numbers
|
||||
|
||||
Rust also has two primitive floating-point numbers: `f32` and `f64`. They are
|
||||
32 bits and 64 bits in size, respectively. The default is `f64`.
|
||||
|
||||
```rust
|
||||
fn main() {
|
||||
let x = 2.0; // f64
|
||||
|
||||
let y: f32 = 3.0; // f32
|
||||
}
|
||||
```
|
||||
|
||||
Floating-point numbers are represented according to the IEEE-754 standard.
|
||||
`f32` is a single-precision float, `f64` is double-precision.
|
||||
Now that we’ve discussed scalar types, let’s talk about compound types.
|
||||
These types can group multiple values of scalar types into another type.
|
||||
|
||||
## Tuples
|
||||
|
||||
The other type we’ve seen previously is the tuple type. Tuples have an ‘arity’,
|
||||
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.”
|
||||
|
||||
Each position in a tuple has a distinct type:
|
||||
@@ -105,143 +49,6 @@ fn main() {
|
||||
}
|
||||
```
|
||||
|
||||
## Functions
|
||||
|
||||
There’s one more type that we’ve been using, but you haven’t seen written
|
||||
explicitly. Functions! Functions also have a type, and yes, you can even have
|
||||
variables which hold functions! Here’s an example:
|
||||
|
||||
```rust
|
||||
fn plus_one(x: i32) -> i32 {
|
||||
x + 1
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let f = plus_one;
|
||||
let g: fn(i32) -> i32 = plus_one; // with an explicit type annotation
|
||||
|
||||
let five = f(4);
|
||||
}
|
||||
```
|
||||
|
||||
As you can see, the type is very similar to the declaration. Here, let’s put
|
||||
them side by side:
|
||||
|
||||
```rust,ignore
|
||||
fn(i32) -> i32 // type
|
||||
fn plus_one(x: i32) -> i32 { // declaration
|
||||
```
|
||||
|
||||
If we take the declaration, and drop the name...
|
||||
|
||||
```rust,ignore
|
||||
fn(i32) -> i32 // type
|
||||
fn(x: i32) -> i32 {
|
||||
```
|
||||
|
||||
And then drop the names of the arguments...
|
||||
|
||||
```rust,ignore
|
||||
fn(i32) -> i32 // type
|
||||
fn(i32) -> i32 {
|
||||
```
|
||||
|
||||
It’s the same! Well, we need to drop that `{` as well.
|
||||
|
||||
Finally, if you’ll notice in that example, we can create a binding with a
|
||||
function in it:
|
||||
|
||||
```rust,ignore
|
||||
fn main() {
|
||||
let f = plus_one;
|
||||
|
||||
let five = f(4);
|
||||
}
|
||||
```
|
||||
|
||||
... and call it with `()`s just like if we had used the original name.
|
||||
|
||||
### Functions as arguments
|
||||
|
||||
So why not just use the original name? Well, we can pass functions as arguments
|
||||
to other functions! Check this out:
|
||||
|
||||
```rust
|
||||
fn plus_one(x: i32) -> i32 {
|
||||
x + 1
|
||||
}
|
||||
|
||||
fn plus_two(x: i32) -> i32 {
|
||||
x + 2
|
||||
}
|
||||
|
||||
fn twice(x: i32, f: fn(i32) -> i32) -> i32 {
|
||||
let mut result = x;
|
||||
|
||||
result = f(result);
|
||||
result = f(result);
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let x = 5;
|
||||
|
||||
let y = twice(x, plus_one);
|
||||
let z = twice(x, plus_two);
|
||||
|
||||
println!("The value of y is: {}", y);
|
||||
println!("The value of z is: {}", z);
|
||||
}
|
||||
```
|
||||
|
||||
If we compile and run this, we’ll get this output:
|
||||
|
||||
```text
|
||||
The value of y is: 7
|
||||
The value of z is: 9
|
||||
```
|
||||
|
||||
Let’s investigate in more detail.
|
||||
|
||||
```rust,ignore
|
||||
fn twice(x: i32, f: fn(i32) -> i32) -> i32 {
|
||||
```
|
||||
|
||||
This says “`twice()` is a function which takes two arguments. `x` is a
|
||||
thirty-two bit integer, and `f` is a function which takes an `i32` and returns
|
||||
an `i32`.”
|
||||
|
||||
Inside of `twice()`, as you might imagine, we call the function `f` twice on
|
||||
`x`, and return the result.
|
||||
|
||||
|
||||
```rust,ignore
|
||||
let y = twice(x, plus_one);
|
||||
let z = twice(x, plus_two);
|
||||
```
|
||||
|
||||
The first time we call `twice()`, we pass `plus_one()` as an argument. And `x`
|
||||
is `5`. So `5 + 1 + 1 == 7`, hence our first line of output. The second time,
|
||||
we pass `plus_two()` instead. `5 + 2 + 2` is `9`, and our second line checks
|
||||
out too.
|
||||
|
||||
Passing functions to functions is very, very powerful.
|
||||
|
||||
## Booleans
|
||||
|
||||
Somewhat fundamental to all computing, Rust has a boolean type, `bool`, with
|
||||
two possible values:
|
||||
|
||||
```rust
|
||||
fn main() {
|
||||
let t = true;
|
||||
let f: bool = false; // with explict type annotation
|
||||
}
|
||||
```
|
||||
|
||||
That’s really all there is to say about that!
|
||||
|
||||
## Arrays
|
||||
|
||||
So far, we’ve only represented single values in a binding. Sometimes, though,
|
||||
@@ -420,46 +227,3 @@ a is [1, 2, 3, 4, 5]
|
||||
|
||||
You’ll see this repeated later, with other types. And we’ll cover traits fully
|
||||
later in the book, Section 9.
|
||||
|
||||
## char
|
||||
|
||||
We’ve only worked with numbers so far, but what about letters? Rust’s most
|
||||
primitive alphabetic type is the `char`:
|
||||
|
||||
```rust
|
||||
fn main() {
|
||||
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
|
||||
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!
|
||||
|
||||
## str
|
||||
|
||||
We can declare literal strings with `"`s. We’ve seen them already, with
|
||||
`println!`:
|
||||
|
||||
```rust
|
||||
fn main() {
|
||||
println!("println! takes a literal string as an argument.");
|
||||
|
||||
let s = "We can also create bindings to string literals.";
|
||||
|
||||
let s: &str = "Here’s one with a type annotation.";
|
||||
}
|
||||
```
|
||||
|
||||
String literals are immutable, and of a fixed length. Rust has a second string
|
||||
type, `String`, that we’ll discuss in section 8.
|
||||
|
||||
`&str`s are UTF-8 encoded.
|
||||
@@ -0,0 +1,96 @@
|
||||
# Scalar Types
|
||||
|
||||
We’ve seen that every value in Rust has a type of some kind. There are a number
|
||||
of types which are built into the language itself. First, we’ll take a look at
|
||||
‘scalar’ types, that is, types which represent a single value.
|
||||
|
||||
Remember, you can rely on type inference to figure out the type of a binding,
|
||||
or you can annotate it explicitly:
|
||||
|
||||
```rust
|
||||
fn main() {
|
||||
let x: i32 = 5;
|
||||
}
|
||||
```
|
||||
|
||||
## Integers
|
||||
|
||||
You’ve already seen one primitive type: `i32`. There are a number of built-in
|
||||
number types in Rust.
|
||||
|
||||
Here’s a chart of Rust’s integer types:
|
||||
|
||||
| | signed | unsigned |
|
||||
|--------|--------|----------|
|
||||
| 8-bit | i8 | u8 |
|
||||
| 16-bit | i16 | u16 |
|
||||
| 32-bit | i32 | u32 |
|
||||
| 64-bit | i64 | u64 |
|
||||
| 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
|
||||
positive or negative. (Think ‘plus sign’ or ‘minus sign’: that’s a signed
|
||||
number.) Signed numbers are stored using ‘two’s compliment’ 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
|
||||
bits, and if you’re on a 32-bit one, they’re 32 bits.
|
||||
|
||||
So how do you choose from all these options? Well, if you really don’t know,
|
||||
the defaults are a good choice: integer types default to `i32`. The primary use
|
||||
case for `isize`/`usize` is when indexing some sort of collection. We’ll talk
|
||||
more about our first collection, arrays, in just a moment.
|
||||
|
||||
## Floating-point numbers
|
||||
|
||||
Rust also has two primitive floating-point numbers: `f32` and `f64`. They are
|
||||
32 bits and 64 bits in size, respectively. The default is `f64`.
|
||||
|
||||
```rust
|
||||
fn main() {
|
||||
let x = 2.0; // f64
|
||||
|
||||
let y: f32 = 3.0; // f32
|
||||
}
|
||||
```
|
||||
|
||||
Floating-point numbers are represented according to the IEEE-754 standard.
|
||||
`f32` is a single-precision float, `f64` is double-precision.
|
||||
|
||||
## Booleans
|
||||
|
||||
Somewhat fundamental to all computing, Rust has a boolean type, `bool`, with
|
||||
two possible values:
|
||||
|
||||
```rust
|
||||
fn main() {
|
||||
let t = true;
|
||||
let f: bool = false; // with explict type annotation
|
||||
}
|
||||
```
|
||||
|
||||
That’s really all there is to say about that!
|
||||
|
||||
## char
|
||||
|
||||
We’ve only worked with numbers so far, but what about letters? Rust’s most
|
||||
primitive alphabetic type is the `char`:
|
||||
|
||||
```rust
|
||||
fn main() {
|
||||
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
|
||||
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!
|
||||
Reference in New Issue
Block a user