doc: Mention tuple structs

/cc: #4217
This commit is contained in:
Tim Chevalier 2012-12-19 21:52:03 -07:00
parent b99a2542f3
commit 8554d5e710
2 changed files with 37 additions and 1 deletions

View File

@ -1072,6 +1072,15 @@ let p = Point {x: 10, y: 11};
let px: int = p.x; let px: int = p.x;
~~~~ ~~~~
A _tuple structure_ is a nominal [tuple type](#tuple-types), also defined with the keyword `struct`.
For example:
~~~~
struct Point(int, int);
let p = Point(10, 11);
let px: int = match p { Point(x, _) => x };
~~~~
### Enumerations ### Enumerations
An _enumeration_ is a simultaneous definition of a nominal [enumerated type](#enumerated-types) as well as a set of *constructors*, 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.
~~~~~~~~{.ebnf .gram} ~~~~~~~~{.ebnf .gram}
struct_expr : expr_path '{' ident ':' expr struct_expr : expr_path '{' ident ':' expr
[ ',' ident ':' expr ] * [ ',' ident ':' expr ] *
[ ".." expr ] '}' [ ".." expr ] '}' |
expr_path '(' expr
[ ',' expr ] * ')'
~~~~~~~~ ~~~~~~~~
There are several forms of structure expressions.
A _structure expression_ consists of the [path](#paths) of a [structure item](#structures), A _structure expression_ consists of the [path](#paths) of a [structure item](#structures),
followed by a brace-enclosed list of one or more comma-separated name-value pairs, followed by a brace-enclosed list of one or more comma-separated name-value pairs,
providing the field values of a new instance of the structure. providing the field values of a new instance of the structure.
A field name can be any identifier, and is separated from its value expression by a colon. A field name can be any identifier, and is separated from its value expression by a colon.
To indicate that a field is mutable, the `mut` keyword is written before its name. To indicate that a field is mutable, the `mut` keyword is written before its name.
A _tuple structure expression_ constists of the [path](#paths) of a [structure item](#structures),
followed by a parenthesized list of one or more comma-separated expressions
(in other words, the path of a structured item followed by a tuple expression).
The structure item must be a tuple structure item.
The following are examples of structure expressions: The following are examples of structure expressions:
~~~~ ~~~~
# struct Point { x: float, y: float } # struct Point { x: float, y: float }
# struct TuplePoint(float, float);
# mod game { pub struct User { name: &str, age: uint, mut score: uint } } # mod game { pub struct User { name: &str, age: uint, mut score: uint } }
# use game; # use game;
Point {x: 10f, y: 20f}; Point {x: 10f, y: 20f};
TuplePoint(10f, 20f);
let u = game::User {name: "Joe", age: 35u, mut score: 100_000}; let u = game::User {name: "Joe", age: 35u, mut score: 100_000};
~~~~ ~~~~
@ -2597,6 +2616,7 @@ the resulting `struct` value will always be laid out in memory in the order spec
The fields of a `struct` may be qualified by [visibility modifiers](#visibility-modifiers), The fields of a `struct` may be qualified by [visibility modifiers](#visibility-modifiers),
to restrict access to implementation-private data in a structure. to restrict access to implementation-private data in a structure.
A `tuple struct` type is just like a structure type, except that the fields are anonymous.
### Enumerated types ### Enumerated types

View File

@ -902,6 +902,22 @@ match mytup {
} }
~~~~ ~~~~
## Tuple structs
Rust also has _nominal tuples_, which behave like both structs and tuples,
except that nominal tuple types have names
(so `Foo(1, 2)` has a different type from `Bar(1, 2)`),
and nominal tuple types' _fields_ do not have names.
For example:
~~~~
struct MyTup(int, int, float);
let mytup: MyTup = MyTup(10, 20, 30.0);
match mytup {
MyTup(a, b, c) => log(info, a + b + (c as int))
}
~~~~
# Functions and methods # Functions and methods
We've already seen several function definitions. Like all other static We've already seen several function definitions. Like all other static