Skip to content

Commit 8554d5e

Browse files
committed
doc: Mention tuple structs
/cc: #4217
1 parent b99a254 commit 8554d5e

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

doc/rust.md

+21-1
Original file line numberDiff line numberDiff line change
@@ -1072,6 +1072,15 @@ let p = Point {x: 10, y: 11};
10721072
let px: int = p.x;
10731073
~~~~
10741074

1075+
A _tuple structure_ is a nominal [tuple type](#tuple-types), also defined with the keyword `struct`.
1076+
For example:
1077+
1078+
~~~~
1079+
struct Point(int, int);
1080+
let p = Point(10, 11);
1081+
let px: int = match p { Point(x, _) => x };
1082+
~~~~
1083+
10751084
### Enumerations
10761085

10771086
An _enumeration_ is a simultaneous definition of a nominal [enumerated type](#enumerated-types) as well as a set of *constructors*,
@@ -1534,22 +1543,32 @@ values.
15341543
~~~~~~~~{.ebnf .gram}
15351544
struct_expr : expr_path '{' ident ':' expr
15361545
[ ',' ident ':' expr ] *
1537-
[ ".." expr ] '}'
1546+
[ ".." expr ] '}' |
1547+
expr_path '(' expr
1548+
[ ',' expr ] * ')'
15381549
~~~~~~~~
15391550

1551+
There are several forms of structure expressions.
15401552
A _structure expression_ consists of the [path](#paths) of a [structure item](#structures),
15411553
followed by a brace-enclosed list of one or more comma-separated name-value pairs,
15421554
providing the field values of a new instance of the structure.
15431555
A field name can be any identifier, and is separated from its value expression by a colon.
15441556
To indicate that a field is mutable, the `mut` keyword is written before its name.
15451557

1558+
A _tuple structure expression_ constists of the [path](#paths) of a [structure item](#structures),
1559+
followed by a parenthesized list of one or more comma-separated expressions
1560+
(in other words, the path of a structured item followed by a tuple expression).
1561+
The structure item must be a tuple structure item.
1562+
15461563
The following are examples of structure expressions:
15471564

15481565
~~~~
15491566
# struct Point { x: float, y: float }
1567+
# struct TuplePoint(float, float);
15501568
# mod game { pub struct User { name: &str, age: uint, mut score: uint } }
15511569
# use game;
15521570
Point {x: 10f, y: 20f};
1571+
TuplePoint(10f, 20f);
15531572
let u = game::User {name: "Joe", age: 35u, mut score: 100_000};
15541573
~~~~
15551574

@@ -2597,6 +2616,7 @@ the resulting `struct` value will always be laid out in memory in the order spec
25972616
The fields of a `struct` may be qualified by [visibility modifiers](#visibility-modifiers),
25982617
to restrict access to implementation-private data in a structure.
25992618

2619+
A `tuple struct` type is just like a structure type, except that the fields are anonymous.
26002620

26012621
### Enumerated types
26022622

doc/tutorial.md

+16
Original file line numberDiff line numberDiff line change
@@ -902,6 +902,22 @@ match mytup {
902902
}
903903
~~~~
904904

905+
## Tuple structs
906+
907+
Rust also has _nominal tuples_, which behave like both structs and tuples,
908+
except that nominal tuple types have names
909+
(so `Foo(1, 2)` has a different type from `Bar(1, 2)`),
910+
and nominal tuple types' _fields_ do not have names.
911+
912+
For example:
913+
~~~~
914+
struct MyTup(int, int, float);
915+
let mytup: MyTup = MyTup(10, 20, 30.0);
916+
match mytup {
917+
MyTup(a, b, c) => log(info, a + b + (c as int))
918+
}
919+
~~~~
920+
905921
# Functions and methods
906922

907923
We've already seen several function definitions. Like all other static

0 commit comments

Comments
 (0)