Skip to content

Commit

Permalink
Introduce the unit type with tuples. Fixes #1933.
Browse files Browse the repository at this point in the history
  • Loading branch information
carols10cents committed Jul 21, 2021
1 parent c6df5a6 commit 96535e0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
5 changes: 5 additions & 0 deletions src/ch03-02-data-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,11 @@ This program creates a tuple, `x`, and then makes new variables for each
element by using their respective indices. As with most programming languages,
the first index in a tuple is 0.

The tuple without any values, `()`, is a special type that has only one value,
also written `()`. The type is called the *unit type* and the value is called
the *unit value*. Expressions implicitly return the unit value if they don't
return any other value.

#### The Array Type

Another way to have a collection of multiple values is with an *array*. Unlike
Expand Down
22 changes: 13 additions & 9 deletions src/ch05-01-defining-structs.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
## Defining and Instantiating Structs

Structs are similar to tuples, which were discussed in Chapter 3. Like tuples,
the pieces of a struct can be different types. Unlike with tuples, you’ll name
each piece of data so it’s clear what the values mean. As a result of these
names, structs are more flexible than tuples: you don’t have to rely on the
order of the data to specify or access the values of an instance.
Structs are similar to tuples, which were discussed in [“The Tuple
Type”][tuples]<!-- ignore --> section. Like tuples, the pieces of a struct can
be different types. Unlike with tuples, you’ll name each piece of data so it’s
clear what the values mean. As a result of these names, structs are more
flexible than tuples: you don’t have to rely on the order of the data to
specify or access the values of an instance.

To define a struct, we enter the keyword `struct` and name the entire struct. A
struct’s name should describe the significance of the pieces of data being
Expand Down Expand Up @@ -151,10 +152,11 @@ individual value, and so on.
### Unit-Like Structs Without Any Fields

You can also define structs that don’t have any fields! These are called
*unit-like structs* because they behave similarly to `()`, the unit type.
Unit-like structs can be useful in situations in which you need to implement a
trait on some type but don’t have any data that you want to store in the type
itself. We’ll discuss traits in Chapter 10.
*unit-like structs* because they behave similarly to `()`, the unit type that
we mentioned in [“The Tuple Type”][tuples]<!-- ignore --> section. Unit-like
structs can be useful in situations in which you need to implement a trait on
some type but don’t have any data that you want to store in the type itself.
We’ll discuss traits in Chapter 10.

> ### Ownership of Struct Data
>
Expand Down Expand Up @@ -239,3 +241,5 @@ after running update-rustc.sh:
pbcopy < listings/ch05-using-structs-to-structure-related-data/no-listing-02-reference-in-struct/output.txt
paste above
add `> ` before every line -->
[tuples]: ch03-02-data-types.html#the-tuple-type

0 comments on commit 96535e0

Please sign in to comment.