Auto merge of #28182 - jackwilsonv:patch-2, r=steveklabnik

r? @steveklabnik

##### About the `struct` section specifically:
I wasn't sure how you'd feel about the first instance since it was originally capitalized, happy to change it back if you think that's better.

Also, I left 'tuple struct' as is since together it isn't a keyword. The first instance currently has single quotes but the others have nothing. I think that feels right.

##### Generally:
I'm working through the book now and I'm happy to keep updating this branch with any formatting tweaks or updates I find if that's easier for you guys, otherwise I'll just create smaller PRs as I go. Just let me know.
This commit is contained in:
bors 2015-09-03 04:43:40 +00:00
commit 1661947014

View file

@ -1,6 +1,6 @@
% Structs % Structs
Structs are a way of creating more complex data types. For example, if we were `struct`s are a way of creating more complex data types. For example, if we were
doing calculations involving coordinates in 2D space, we would need both an `x` doing calculations involving coordinates in 2D space, we would need both an `x`
and a `y` value: and a `y` value:
@ -9,7 +9,7 @@ let origin_x = 0;
let origin_y = 0; let origin_y = 0;
``` ```
A struct lets us combine these two into a single, unified datatype: A `struct` lets us combine these two into a single, unified datatype:
```rust ```rust
struct Point { struct Point {
@ -28,14 +28,14 @@ Theres a lot going on here, so lets break it down. We declare a `struct` w
the `struct` keyword, and then with a name. By convention, `struct`s begin with the `struct` keyword, and then with a name. By convention, `struct`s begin with
a capital letter and are camel cased: `PointInSpace`, not `Point_In_Space`. a capital letter and are camel cased: `PointInSpace`, not `Point_In_Space`.
We can create an instance of our struct via `let`, as usual, but we use a `key: We can create an instance of our `struct` via `let`, as usual, but we use a `key:
value` style syntax to set each field. The order doesnt need to be the same as value` style syntax to set each field. The order doesnt need to be the same as
in the original declaration. in the original declaration.
Finally, because fields have names, we can access the field through dot Finally, because fields have names, we can access the field through dot
notation: `origin.x`. notation: `origin.x`.
The values in structs are immutable by default, like other bindings in Rust. The values in `struct`s are immutable by default, like other bindings in Rust.
Use `mut` to make them mutable: Use `mut` to make them mutable:
```rust ```rust
@ -91,7 +91,7 @@ fn main() {
# Update syntax # Update syntax
A `struct` can include `..` to indicate that you want to use a copy of some A `struct` can include `..` to indicate that you want to use a copy of some
other struct for some of the values. For example: other `struct` for some of the values. For example:
```rust ```rust
struct Point3d { struct Point3d {
@ -121,7 +121,7 @@ let point = Point3d { z: 1, x: 2, .. origin };
# Tuple structs # Tuple structs
Rust has another data type thats like a hybrid between a [tuple][tuple] and a Rust has another data type thats like a hybrid between a [tuple][tuple] and a
struct, called a tuple struct. Tuple structs have a name, but `struct`, called a tuple struct. Tuple structs have a name, but
their fields dont: their fields dont:
```rust ```rust
@ -140,7 +140,7 @@ let black = Color(0, 0, 0);
let origin = Point(0, 0, 0); let origin = Point(0, 0, 0);
``` ```
It is almost always better to use a struct than a tuple struct. We would write It is almost always better to use a `struct` than a tuple struct. We would write
`Color` and `Point` like this instead: `Color` and `Point` like this instead:
```rust ```rust
@ -158,7 +158,7 @@ struct Point {
``` ```
Now, we have actual names, rather than positions. Good names are important, Now, we have actual names, rather than positions. Good names are important,
and with a struct, we have actual names. and with a `struct`, we have actual names.
There _is_ one case when a tuple struct is very useful, though, and thats a There _is_ one case when a tuple struct is very useful, though, and thats a
tuple struct with only one element. We call this the newtype pattern, because tuple struct with only one element. We call this the newtype pattern, because
@ -180,13 +180,13 @@ destructuring `let`, just as with regular tuples. In this case, the
# Unit-like structs # Unit-like structs
You can define a struct with no members at all: You can define a `struct` with no members at all:
```rust ```rust
struct Electron; struct Electron;
``` ```
Such a struct is called unit-like because it resembles the empty Such a `struct` is called unit-like because it resembles the empty
tuple, `()`, sometimes called unit. Like a tuple struct, it defines a tuple, `()`, sometimes called unit. Like a tuple struct, it defines a
new type. new type.
@ -195,6 +195,6 @@ marker type), but in combination with other features, it can become
useful. For instance, a library may ask you to create a structure that useful. For instance, a library may ask you to create a structure that
implements a certain [trait][trait] to handle events. If you dont have implements a certain [trait][trait] to handle events. If you dont have
any data you need to store in the structure, you can just create a any data you need to store in the structure, you can just create a
unit-like struct. unit-like `struct`.
[trait]: traits.html [trait]: traits.html