Skip to content

Commit 714bd49

Browse files
committed
Auto merge of #24744 - bluss:reference-1, r=steveklabnik
Audit & Edit Chapter 8.1 Types in reference manual - Remove mention of unit type - Update closure types and reference types sections - Fix minor typos
2 parents f191f92 + b22ea2d commit 714bd49

File tree

1 file changed

+30
-49
lines changed

1 file changed

+30
-49
lines changed

src/doc/reference.md

+30-49
Original file line numberDiff line numberDiff line change
@@ -3430,17 +3430,10 @@ User-defined types have limited capabilities.
34303430

34313431
The primitive types are the following:
34323432

3433-
* The "unit" type `()`, having the single "unit" value `()` (occasionally called
3434-
"nil"). [^unittype]
34353433
* The boolean type `bool` with values `true` and `false`.
34363434
* The machine types.
34373435
* The machine-dependent integer and floating-point types.
34383436

3439-
[^unittype]: The "unit" value `()` is *not* a sentinel "null pointer" value for
3440-
reference variables; the "unit" type is the implicit return type from functions
3441-
otherwise lacking a return type, and can be used in other contexts (such as
3442-
message-sending or type-parametric code) as a zero-size type.]
3443-
34443437
#### Machine types
34453438

34463439
The machine types are the following:
@@ -3481,7 +3474,7 @@ UTF-32 string.
34813474
A value of type `str` is a Unicode string, represented as an array of 8-bit
34823475
unsigned bytes holding a sequence of UTF-8 code points. Since `str` is of
34833476
unknown size, it is not a _first-class_ type, but can only be instantiated
3484-
through a pointer type, such as `&str` or `String`.
3477+
through a pointer type, such as `&str`.
34853478

34863479
### Tuple types
34873480

@@ -3537,7 +3530,7 @@ to an array or slice is always bounds-checked.
35373530
A `struct` *type* is a heterogeneous product of other types, called the
35383531
*fields* of the type.[^structtype]
35393532

3540-
[^structtype]: `struct` types are analogous `struct` types in C,
3533+
[^structtype]: `struct` types are analogous to `struct` types in C,
35413534
the *record* types of the ML family,
35423535
or the *structure* types of the Lisp family.
35433536

@@ -3551,7 +3544,7 @@ a corresponding struct *expression*; the resulting `struct` value will always
35513544
have the same memory layout.
35523545

35533546
The fields of a `struct` may be qualified by [visibility
3554-
modifiers](#re-exporting-and-visibility), to allow access to data in a
3547+
modifiers](#visibility-and-privacy), to allow access to data in a
35553548
structure outside a module.
35563549

35573550
A _tuple struct_ type is just like a structure type, except that the fields are
@@ -3619,18 +3612,18 @@ varieties of pointer in Rust:
36193612

36203613
* References (`&`)
36213614
: These point to memory _owned by some other value_.
3622-
A reference type is written `&type` for some lifetime-variable `f`,
3623-
or just `&'a type` when you need an explicit lifetime.
3615+
A reference type is written `&type`,
3616+
or `&'a type` when you need to specify an explicit lifetime.
36243617
Copying a reference is a "shallow" operation:
36253618
it involves only copying the pointer itself.
3626-
Releasing a reference typically has no effect on the value it points to,
3627-
with the exception of temporary values, which are released when the last
3628-
reference to them is released.
3619+
Releasing a reference has no effect on the value it points to,
3620+
but a reference of a temporary value will keep it alive during the scope
3621+
of the reference itself.
36293622

36303623
* Raw pointers (`*`)
36313624
: Raw pointers are pointers without safety or liveness guarantees.
36323625
Raw pointers are written as `*const T` or `*mut T`,
3633-
for example `*const int` means a raw pointer to an integer.
3626+
for example `*const i32` means a raw pointer to a 32-bit integer.
36343627
Copying or dropping a raw pointer has no effect on the lifecycle of any
36353628
other value. Dereferencing a raw pointer or converting it to any other
36363629
pointer type is an [`unsafe` operation](#unsafe-functions).
@@ -3663,38 +3656,26 @@ x = bo(5,7);
36633656

36643657
### Closure types
36653658

3666-
```{.ebnf .notation}
3667-
closure_type := [ 'unsafe' ] [ '<' lifetime-list '>' ] '|' arg-list '|'
3668-
[ ':' bound-list ] [ '->' type ]
3669-
lifetime-list := lifetime | lifetime ',' lifetime-list
3670-
arg-list := ident ':' type | ident ':' type ',' arg-list
3671-
bound-list := bound | bound '+' bound-list
3672-
bound := path | lifetime
3673-
```
3674-
3675-
The type of a closure mapping an input of type `A` to an output of type `B` is
3676-
`|A| -> B`. A closure with no arguments or return values has type `||`.
3677-
3678-
An example of creating and calling a closure:
3659+
A [lambda expression](#lambda-expressions) produces a closure value with
3660+
a unique, anonymous type that cannot be written out.
36793661

3680-
```rust
3681-
let captured_var = 10;
3662+
Depending on the requirements of the closure, its type implements one or
3663+
more of the closure traits:
36823664

3683-
let closure_no_args = || println!("captured_var={}", captured_var);
3665+
* `FnOnce`
3666+
: The closure can be called once. A closure called as `FnOnce`
3667+
can move out values from its environment.
36843668

3685-
let closure_args = |arg: i32| -> i32 {
3686-
println!("captured_var={}, arg={}", captured_var, arg);
3687-
arg // Note lack of semicolon after 'arg'
3688-
};
3669+
* `FnMut`
3670+
: The closure can be called multiple times as mutable. A closure called as
3671+
`FnMut` can mutate values from its environment. `FnMut` implies
3672+
`FnOnce`.
36893673

3690-
fn call_closure<F: Fn(), G: Fn(i32) -> i32>(c1: F, c2: G) {
3691-
c1();
3692-
c2(2);
3693-
}
3674+
* `Fn`
3675+
: The closure can be called multiple times through a shared reference.
3676+
A closure called as `Fn` can neither move out from nor mutate values
3677+
from its environment. `Fn` implies `FnMut` and `FnOnce`.
36943678

3695-
call_closure(closure_no_args, closure_args);
3696-
3697-
```
36983679

36993680
### Trait objects
37003681

@@ -3741,19 +3722,19 @@ Within the body of an item that has type parameter declarations, the names of
37413722
its type parameters are types:
37423723

37433724
```ignore
3744-
fn map<A: Clone, B: Clone>(f: |A| -> B, xs: &[A]) -> Vec<B> {
3725+
fn to_vec<A: Clone>(xs: &[A]) -> Vec<A> {
37453726
if xs.is_empty() {
37463727
return vec![];
37473728
}
3748-
let first: B = f(xs[0].clone());
3749-
let mut rest: Vec<B> = map(f, xs.slice(1, xs.len()));
3729+
let first: A = xs[0].clone();
3730+
let mut rest: Vec<A> = to_vec(&xs[1..]);
37503731
rest.insert(0, first);
3751-
return rest;
3732+
rest
37523733
}
37533734
```
37543735

3755-
Here, `first` has type `B`, referring to `map`'s `B` type parameter; and `rest`
3756-
has type `Vec<B>`, a vector type with element type `B`.
3736+
Here, `first` has type `A`, referring to `to_vec`'s `A` type parameter; and `rest`
3737+
has type `Vec<A>`, a vector with element type `A`.
37573738

37583739
### Self types
37593740

0 commit comments

Comments
 (0)