From 457aed7ca003d7cbef2cda8360ad9b8b5bb83650 Mon Sep 17 00:00:00 2001 From: Geoffrey Thomas Date: Tue, 12 May 2015 14:13:03 -0400 Subject: [PATCH] trpl: move tuple-structs.md into structs.md --- src/doc/trpl/SUMMARY.md | 1 - src/doc/trpl/structs.md | 60 +++++++++++++++++++++++++++++++++++ src/doc/trpl/tuple-structs.md | 60 ----------------------------------- 3 files changed, 60 insertions(+), 61 deletions(-) delete mode 100644 src/doc/trpl/tuple-structs.md diff --git a/src/doc/trpl/SUMMARY.md b/src/doc/trpl/SUMMARY.md index de7ded76280f..2ab71189ff8f 100644 --- a/src/doc/trpl/SUMMARY.md +++ b/src/doc/trpl/SUMMARY.md @@ -43,7 +43,6 @@ * [Universal Function Call Syntax](ufcs.md) * [Crates and Modules](crates-and-modules.md) * [`const` and `static`](const-and-static.md) - * [Tuple Structs](tuple-structs.md) * [Attributes](attributes.md) * [`type` aliases](type-aliases.md) * [Casting between types](casting-between-types.md) diff --git a/src/doc/trpl/structs.md b/src/doc/trpl/structs.md index fcf928e427c2..2f8d2bfdea0a 100644 --- a/src/doc/trpl/structs.md +++ b/src/doc/trpl/structs.md @@ -117,3 +117,63 @@ ones, and it will copy the values you don’t specify: let origin = Point3d { x: 0, y: 0, z: 0 }; let point = Point3d { z: 1, x: 2, .. origin }; ``` + +# Tuple structs + +Rust has another data type that’s like a hybrid between a [tuple][tuple] and a +struct, called a ‘tuple struct’. Tuple structs have a name, but +their fields don’t: + +```rust +struct Color(i32, i32, i32); +struct Point(i32, i32, i32); +``` + +[tuple]: primitive-types.html#tuples + +These two will not be equal, even if they have the same values: + +```rust +# struct Color(i32, i32, i32); +# struct Point(i32, i32, i32); +let black = Color(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 +`Color` and `Point` like this instead: + +```rust +struct Color { + red: i32, + blue: i32, + green: i32, +} + +struct Point { + x: i32, + y: i32, + z: i32, +} +``` + +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 +it allows you to create a new type, distinct from that of its contained value +and expressing its own semantic meaning: + +```rust +struct Inches(i32); + +let length = Inches(10); + +let Inches(integer_length) = length; +println!("length is {} inches", integer_length); +``` + +As you can see here, you can extract the inner integer type through a +destructuring `let`, just as with regular tuples. In this case, the +`let Inches(integer_length)` assigns `10` to `integer_length`. diff --git a/src/doc/trpl/tuple-structs.md b/src/doc/trpl/tuple-structs.md deleted file mode 100644 index bdaef70711ae..000000000000 --- a/src/doc/trpl/tuple-structs.md +++ /dev/null @@ -1,60 +0,0 @@ -% Tuple Structs - -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 -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 -# struct Color(i32, i32, i32); -# struct Point(i32, i32, i32); -let black = Color(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 -`Color` and `Point` like this instead: - -```rust -struct Color { - red: i32, - blue: i32, - green: i32, -} - -struct Point { - x: i32, - y: i32, - z: i32, -} -``` - -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 -it allows you to create a new type, distinct from that of its contained value -and expressing its own semantic meaning: - -```rust -struct Inches(i32); - -let length = Inches(10); - -let Inches(integer_length) = length; -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 -`let Inches(integer_length)` assigns `10` to `integer_length`.