Skip to content

Commit aa93c11

Browse files
author
Ulrik Sverdrup
committed
reference: Update reference types, closure types, type parameters
1 parent 7525201 commit aa93c11

File tree

1 file changed

+27
-39
lines changed

1 file changed

+27
-39
lines changed

src/doc/reference.md

Lines changed: 27 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3573,18 +3573,18 @@ varieties of pointer in Rust:
35733573

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

35843584
* Raw pointers (`*`)
35853585
: Raw pointers are pointers without safety or liveness guarantees.
35863586
Raw pointers are written as `*const T` or `*mut T`,
3587-
for example `*const int` means a raw pointer to an integer.
3587+
for example `*const i32` means a raw pointer to a 32-bit integer.
35883588
Copying or dropping a raw pointer has no effect on the lifecycle of any
35893589
other value. Dereferencing a raw pointer or converting it to any other
35903590
pointer type is an [`unsafe` operation](#unsafe-functions).
@@ -3617,38 +3617,26 @@ x = bo(5,7);
36173617

36183618
### Closure types
36193619

3620-
```{.ebnf .notation}
3621-
closure_type := [ 'unsafe' ] [ '<' lifetime-list '>' ] '|' arg-list '|'
3622-
[ ':' bound-list ] [ '->' type ]
3623-
lifetime-list := lifetime | lifetime ',' lifetime-list
3624-
arg-list := ident ':' type | ident ':' type ',' arg-list
3625-
bound-list := bound | bound '+' bound-list
3626-
bound := path | lifetime
3627-
```
3620+
A [lambda expression](#lambda-expressions) produces a closure value with
3621+
a unique, anonymous type that cannot be written out.
36283622

3629-
The type of a closure mapping an input of type `A` to an output of type `B` is
3630-
`|A| -> B`. A closure with no arguments or return values has type `||`.
3623+
Depending on the requirements of the closure, its type implements one or
3624+
more of the closure traits:
36313625

3632-
An example of creating and calling a closure:
3626+
* `FnOnce`
3627+
: The closure can be called at least once. A closure called as `FnOnce`
3628+
can move out values from its environment.
36333629

3634-
```rust
3635-
let captured_var = 10;
3630+
* `FnMut`
3631+
: The closure can be called multiple times as mutable. A closure called as
3632+
`FnMut` can mutate values from its environment. `FnMut` implies
3633+
`FnOnce`.
36363634

3637-
let closure_no_args = || println!("captured_var={}", captured_var);
3638-
3639-
let closure_args = |arg: i32| -> i32 {
3640-
println!("captured_var={}, arg={}", captured_var, arg);
3641-
arg // Note lack of semicolon after 'arg'
3642-
};
3635+
* `Fn`
3636+
: The closure can be called multiple times through a shared reference.
3637+
A closure called as `Fn` can neither move out from nor mutate values
3638+
from its environment. `Fn` implies `FnMut` and `FnOnce`.
36433639

3644-
fn call_closure<F: Fn(), G: Fn(i32) -> i32>(c1: F, c2: G) {
3645-
c1();
3646-
c2(2);
3647-
}
3648-
3649-
call_closure(closure_no_args, closure_args);
3650-
3651-
```
36523640

36533641
### Object types
36543642

@@ -3695,19 +3683,19 @@ Within the body of an item that has type parameter declarations, the names of
36953683
its type parameters are types:
36963684

36973685
```ignore
3698-
fn map<A: Clone, B: Clone>(f: |A| -> B, xs: &[A]) -> Vec<B> {
3686+
fn to_vec<A: Clone>(xs: &[A]) -> Vec<A> {
36993687
if xs.is_empty() {
37003688
return vec![];
37013689
}
3702-
let first: B = f(xs[0].clone());
3703-
let mut rest: Vec<B> = map(f, xs.slice(1, xs.len()));
3690+
let first: A = xs[0].clone();
3691+
let mut rest: Vec<A> = to_vec(&xs[1..]);
37043692
rest.insert(0, first);
3705-
return rest;
3693+
rest
37063694
}
37073695
```
37083696

3709-
Here, `first` has type `B`, referring to `map`'s `B` type parameter; and `rest`
3710-
has type `Vec<B>`, a vector type with element type `B`.
3697+
Here, `first` has type `A`, referring to `to_vec`'s `A` type parameter; and `rest`
3698+
has type `Vec<A>`, a vector with element type `A`.
37113699

37123700
### Self types
37133701

0 commit comments

Comments
 (0)