Skip to content

Commit b22ea2d

Browse files
author
Ulrik Sverdrup
committed
reference: Audit & Edit chapter 8.1 Types.
- Remove mention of unit type - Update closure types and reference types sections - Fix minor typos
1 parent 69e47c7 commit b22ea2d

File tree

1 file changed

+30
-49
lines changed

1 file changed

+30
-49
lines changed

src/doc/reference.md

Lines changed: 30 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -3383,17 +3383,10 @@ User-defined types have limited capabilities.
33833383

33843384
The primitive types are the following:
33853385

3386-
* The "unit" type `()`, having the single "unit" value `()` (occasionally called
3387-
"nil"). [^unittype]
33883386
* The boolean type `bool` with values `true` and `false`.
33893387
* The machine types.
33903388
* The machine-dependent integer and floating-point types.
33913389

3392-
[^unittype]: The "unit" value `()` is *not* a sentinel "null pointer" value for
3393-
reference variables; the "unit" type is the implicit return type from functions
3394-
otherwise lacking a return type, and can be used in other contexts (such as
3395-
message-sending or type-parametric code) as a zero-size type.]
3396-
33973390
#### Machine types
33983391

33993392
The machine types are the following:
@@ -3434,7 +3427,7 @@ UTF-32 string.
34343427
A value of type `str` is a Unicode string, represented as an array of 8-bit
34353428
unsigned bytes holding a sequence of UTF-8 codepoints. Since `str` is of
34363429
unknown size, it is not a _first-class_ type, but can only be instantiated
3437-
through a pointer type, such as `&str` or `String`.
3430+
through a pointer type, such as `&str`.
34383431

34393432
### Tuple types
34403433

@@ -3490,7 +3483,7 @@ to an array or slice is always bounds-checked.
34903483
A `struct` *type* is a heterogeneous product of other types, called the
34913484
*fields* of the type.[^structtype]
34923485

3493-
[^structtype]: `struct` types are analogous `struct` types in C,
3486+
[^structtype]: `struct` types are analogous to `struct` types in C,
34943487
the *record* types of the ML family,
34953488
or the *structure* types of the Lisp family.
34963489

@@ -3504,7 +3497,7 @@ a corresponding struct *expression*; the resulting `struct` value will always
35043497
have the same memory layout.
35053498

35063499
The fields of a `struct` may be qualified by [visibility
3507-
modifiers](#re-exporting-and-visibility), to allow access to data in a
3500+
modifiers](#visibility-and-privacy), to allow access to data in a
35083501
structure outside a module.
35093502

35103503
A _tuple struct_ type is just like a structure type, except that the fields are
@@ -3572,18 +3565,18 @@ varieties of pointer in Rust:
35723565

35733566
* References (`&`)
35743567
: These point to memory _owned by some other value_.
3575-
A reference type is written `&type` for some lifetime-variable `f`,
3576-
or just `&'a type` when you need an explicit lifetime.
3568+
A reference type is written `&type`,
3569+
or `&'a type` when you need to specify an explicit lifetime.
35773570
Copying a reference is a "shallow" operation:
35783571
it involves only copying the pointer itself.
3579-
Releasing a reference typically has no effect on the value it points to,
3580-
with the exception of temporary values, which are released when the last
3581-
reference to them is released.
3572+
Releasing a reference has no effect on the value it points to,
3573+
but a reference of a temporary value will keep it alive during the scope
3574+
of the reference itself.
35823575

35833576
* Raw pointers (`*`)
35843577
: Raw pointers are pointers without safety or liveness guarantees.
35853578
Raw pointers are written as `*const T` or `*mut T`,
3586-
for example `*const int` means a raw pointer to an integer.
3579+
for example `*const i32` means a raw pointer to a 32-bit integer.
35873580
Copying or dropping a raw pointer has no effect on the lifecycle of any
35883581
other value. Dereferencing a raw pointer or converting it to any other
35893582
pointer type is an [`unsafe` operation](#unsafe-functions).
@@ -3616,38 +3609,26 @@ x = bo(5,7);
36163609

36173610
### Closure types
36183611

3619-
```{.ebnf .notation}
3620-
closure_type := [ 'unsafe' ] [ '<' lifetime-list '>' ] '|' arg-list '|'
3621-
[ ':' bound-list ] [ '->' type ]
3622-
lifetime-list := lifetime | lifetime ',' lifetime-list
3623-
arg-list := ident ':' type | ident ':' type ',' arg-list
3624-
bound-list := bound | bound '+' bound-list
3625-
bound := path | lifetime
3626-
```
3627-
3628-
The type of a closure mapping an input of type `A` to an output of type `B` is
3629-
`|A| -> B`. A closure with no arguments or return values has type `||`.
3630-
3631-
An example of creating and calling a closure:
3612+
A [lambda expression](#lambda-expressions) produces a closure value with
3613+
a unique, anonymous type that cannot be written out.
36323614

3633-
```rust
3634-
let captured_var = 10;
3615+
Depending on the requirements of the closure, its type implements one or
3616+
more of the closure traits:
36353617

3636-
let closure_no_args = || println!("captured_var={}", captured_var);
3618+
* `FnOnce`
3619+
: The closure can be called once. A closure called as `FnOnce`
3620+
can move out values from its environment.
36373621

3638-
let closure_args = |arg: i32| -> i32 {
3639-
println!("captured_var={}, arg={}", captured_var, arg);
3640-
arg // Note lack of semicolon after 'arg'
3641-
};
3622+
* `FnMut`
3623+
: The closure can be called multiple times as mutable. A closure called as
3624+
`FnMut` can mutate values from its environment. `FnMut` implies
3625+
`FnOnce`.
36423626

3643-
fn call_closure<F: Fn(), G: Fn(i32) -> i32>(c1: F, c2: G) {
3644-
c1();
3645-
c2(2);
3646-
}
3627+
* `Fn`
3628+
: The closure can be called multiple times through a shared reference.
3629+
A closure called as `Fn` can neither move out from nor mutate values
3630+
from its environment. `Fn` implies `FnMut` and `FnOnce`.
36473631

3648-
call_closure(closure_no_args, closure_args);
3649-
3650-
```
36513632

36523633
### Object types
36533634

@@ -3694,19 +3675,19 @@ Within the body of an item that has type parameter declarations, the names of
36943675
its type parameters are types:
36953676

36963677
```ignore
3697-
fn map<A: Clone, B: Clone>(f: |A| -> B, xs: &[A]) -> Vec<B> {
3678+
fn to_vec<A: Clone>(xs: &[A]) -> Vec<A> {
36983679
if xs.is_empty() {
36993680
return vec![];
37003681
}
3701-
let first: B = f(xs[0].clone());
3702-
let mut rest: Vec<B> = map(f, xs.slice(1, xs.len()));
3682+
let first: A = xs[0].clone();
3683+
let mut rest: Vec<A> = to_vec(&xs[1..]);
37033684
rest.insert(0, first);
3704-
return rest;
3685+
rest
37053686
}
37063687
```
37073688

3708-
Here, `first` has type `B`, referring to `map`'s `B` type parameter; and `rest`
3709-
has type `Vec<B>`, a vector type with element type `B`.
3689+
Here, `first` has type `A`, referring to `to_vec`'s `A` type parameter; and `rest`
3690+
has type `Vec<A>`, a vector with element type `A`.
37103691

37113692
### Self types
37123693

0 commit comments

Comments
 (0)