@@ -3573,18 +3573,18 @@ varieties of pointer in Rust:
3573
3573
3574
3574
* References (` & ` )
3575
3575
: 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.
3578
3578
Copying a reference is a "shallow" operation:
3579
3579
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 .
3583
3583
3584
3584
* Raw pointers (` * ` )
3585
3585
: Raw pointers are pointers without safety or liveness guarantees.
3586
3586
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.
3588
3588
Copying or dropping a raw pointer has no effect on the lifecycle of any
3589
3589
other value. Dereferencing a raw pointer or converting it to any other
3590
3590
pointer type is an [ ` unsafe ` operation] ( #unsafe-functions ) .
@@ -3617,38 +3617,26 @@ x = bo(5,7);
3617
3617
3618
3618
### Closure types
3619
3619
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.
3628
3622
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:
3631
3625
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.
3633
3629
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 ` .
3636
3634
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 ` .
3643
3639
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
- ```
3652
3640
3653
3641
### Object types
3654
3642
@@ -3695,19 +3683,19 @@ Within the body of an item that has type parameter declarations, the names of
3695
3683
its type parameters are types:
3696
3684
3697
3685
``` 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 > {
3699
3687
if xs.is_empty() {
3700
3688
return vec![];
3701
3689
}
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..] );
3704
3692
rest.insert(0, first);
3705
- return rest;
3693
+ rest
3706
3694
}
3707
3695
```
3708
3696
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 ` .
3711
3699
3712
3700
### Self types
3713
3701
0 commit comments