Skip to content

TRPL editing: tuple structs #24672

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 22, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions src/doc/trpl/tuple-structs.md
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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,
Expand All @@ -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 thats 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);
Expand All @@ -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`.