diff options
| author | Alex Crichton <alex@alexcrichton.com> | 2015-04-21 15:23:18 -0700 |
|---|---|---|
| committer | Alex Crichton <alex@alexcrichton.com> | 2015-04-21 15:23:18 -0700 |
| commit | 44338cb944f8a4ac8132929d7546a12291392aab (patch) | |
| tree | 0873bc339389de98dd46ade7f812932d8793fdea /src | |
| parent | cfbc5bebaedc3eb0898eec00ef9dcf8005ab9161 (diff) | |
| parent | b0105b5a59a2c86e7e7c0d602fe58e9ba61f06b6 (diff) | |
| download | rust-44338cb944f8a4ac8132929d7546a12291392aab.tar.gz rust-44338cb944f8a4ac8132929d7546a12291392aab.zip | |
rollup merge of #24672: steveklabnik/edit_tuple_structs
I thought I edited all the last little chapters, but I missed this one. r? @alexcrichton
Diffstat (limited to 'src')
| -rw-r--r-- | src/doc/trpl/tuple-structs.md | 22 |
1 files changed, 13 insertions, 9 deletions
diff --git a/src/doc/trpl/tuple-structs.md b/src/doc/trpl/tuple-structs.md index 8fba658fba2..bdaef70711a 100644 --- a/src/doc/trpl/tuple-structs.md +++ b/src/doc/trpl/tuple-structs.md @@ -1,16 +1,20 @@ % Tuple Structs -Rust has another data type that's like a hybrid between a tuple and a struct, -called a *tuple struct*. Tuple structs do have a name, but their fields don't: +Rust has another data type that's like a hybrid between a [tuple][tuple] and a +[struct][struct], called a ‘tuple struct’. Tuple structs have a name, but +their fields don’t: -```{rust} +```rust struct Color(i32, i32, i32); struct Point(i32, i32, i32); ``` +[tuple]: primitive-types.html#tuples +[struct]: structs.html + These two will not be equal, even if they have the same values: -```{rust} +```rust # struct Color(i32, i32, i32); # struct Point(i32, i32, i32); let black = Color(0, 0, 0); @@ -20,7 +24,7 @@ let origin = Point(0, 0, 0); It is almost always better to use a struct than a tuple struct. We would write `Color` and `Point` like this instead: -```{rust} +```rust struct Color { red: i32, blue: i32, @@ -37,12 +41,12 @@ struct Point { Now, we have actual names, rather than positions. Good names are important, and with a struct, we have actual names. -There _is_ one case when a tuple struct is very useful, though, and that's a -tuple struct with only one element. We call this the *newtype* pattern, because +There _is_ one case when a tuple struct is very useful, though, and that’s a +tuple struct with only one element. We call this the ‘newtype’ pattern, because it allows you to create a new type, distinct from that of its contained value and expressing its own semantic meaning: -```{rust} +```rust struct Inches(i32); let length = Inches(10); @@ -52,5 +56,5 @@ println!("length is {} inches", integer_length); ``` As you can see here, you can extract the inner integer type through a -destructuring `let`, as we discussed previously in 'tuples.' In this case, the +destructuring `let`, as we discussed previously in ‘tuples’. In this case, the `let Inches(integer_length)` assigns `10` to `integer_length`. |
