@@ -3383,17 +3383,10 @@ User-defined types have limited capabilities.
3383
3383
3384
3384
The primitive types are the following:
3385
3385
3386
- * The "unit" type ` () ` , having the single "unit" value ` () ` (occasionally called
3387
- "nil"). [ ^ unittype ]
3388
3386
* The boolean type ` bool ` with values ` true ` and ` false ` .
3389
3387
* The machine types.
3390
3388
* The machine-dependent integer and floating-point types.
3391
3389
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
-
3397
3390
#### Machine types
3398
3391
3399
3392
The machine types are the following:
@@ -3434,7 +3427,7 @@ UTF-32 string.
3434
3427
A value of type ` str ` is a Unicode string, represented as an array of 8-bit
3435
3428
unsigned bytes holding a sequence of UTF-8 codepoints. Since ` str ` is of
3436
3429
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 ` .
3438
3431
3439
3432
### Tuple types
3440
3433
@@ -3490,7 +3483,7 @@ to an array or slice is always bounds-checked.
3490
3483
A ` struct ` * type* is a heterogeneous product of other types, called the
3491
3484
* fields* of the type.[ ^ structtype ]
3492
3485
3493
- [ ^ structtype ] : ` struct ` types are analogous ` struct ` types in C,
3486
+ [ ^ structtype ] : ` struct ` types are analogous to ` struct ` types in C,
3494
3487
the * record* types of the ML family,
3495
3488
or the * structure* types of the Lisp family.
3496
3489
@@ -3504,7 +3497,7 @@ a corresponding struct *expression*; the resulting `struct` value will always
3504
3497
have the same memory layout.
3505
3498
3506
3499
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
3508
3501
structure outside a module.
3509
3502
3510
3503
A _ tuple struct_ type is just like a structure type, except that the fields are
@@ -3572,18 +3565,18 @@ varieties of pointer in Rust:
3572
3565
3573
3566
* References (` & ` )
3574
3567
: 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.
3577
3570
Copying a reference is a "shallow" operation:
3578
3571
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 .
3582
3575
3583
3576
* Raw pointers (` * ` )
3584
3577
: Raw pointers are pointers without safety or liveness guarantees.
3585
3578
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.
3587
3580
Copying or dropping a raw pointer has no effect on the lifecycle of any
3588
3581
other value. Dereferencing a raw pointer or converting it to any other
3589
3582
pointer type is an [ ` unsafe ` operation] ( #unsafe-functions ) .
@@ -3616,38 +3609,26 @@ x = bo(5,7);
3616
3609
3617
3610
### Closure types
3618
3611
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.
3632
3614
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:
3635
3617
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.
3637
3621
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 ` .
3642
3626
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 ` .
3647
3631
3648
- call_closure (closure_no_args , closure_args );
3649
-
3650
- ```
3651
3632
3652
3633
### Object types
3653
3634
@@ -3694,19 +3675,19 @@ Within the body of an item that has type parameter declarations, the names of
3694
3675
its type parameters are types:
3695
3676
3696
3677
``` 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 > {
3698
3679
if xs.is_empty() {
3699
3680
return vec![];
3700
3681
}
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..] );
3703
3684
rest.insert(0, first);
3704
- return rest;
3685
+ rest
3705
3686
}
3706
3687
```
3707
3688
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 ` .
3710
3691
3711
3692
### Self types
3712
3693
0 commit comments