@@ -3430,17 +3430,10 @@ User-defined types have limited capabilities.
3430
3430
3431
3431
The primitive types are the following:
3432
3432
3433
- * The "unit" type ` () ` , having the single "unit" value ` () ` (occasionally called
3434
- "nil"). [ ^ unittype ]
3435
3433
* The boolean type ` bool ` with values ` true ` and ` false ` .
3436
3434
* The machine types.
3437
3435
* The machine-dependent integer and floating-point types.
3438
3436
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
-
3444
3437
#### Machine types
3445
3438
3446
3439
The machine types are the following:
@@ -3481,7 +3474,7 @@ UTF-32 string.
3481
3474
A value of type ` str ` is a Unicode string, represented as an array of 8-bit
3482
3475
unsigned bytes holding a sequence of UTF-8 code points. Since ` str ` is of
3483
3476
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 ` .
3485
3478
3486
3479
### Tuple types
3487
3480
@@ -3537,7 +3530,7 @@ to an array or slice is always bounds-checked.
3537
3530
A ` struct ` * type* is a heterogeneous product of other types, called the
3538
3531
* fields* of the type.[ ^ structtype ]
3539
3532
3540
- [ ^ structtype ] : ` struct ` types are analogous ` struct ` types in C,
3533
+ [ ^ structtype ] : ` struct ` types are analogous to ` struct ` types in C,
3541
3534
the * record* types of the ML family,
3542
3535
or the * structure* types of the Lisp family.
3543
3536
@@ -3551,7 +3544,7 @@ a corresponding struct *expression*; the resulting `struct` value will always
3551
3544
have the same memory layout.
3552
3545
3553
3546
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
3555
3548
structure outside a module.
3556
3549
3557
3550
A _ tuple struct_ type is just like a structure type, except that the fields are
@@ -3619,18 +3612,18 @@ varieties of pointer in Rust:
3619
3612
3620
3613
* References (` & ` )
3621
3614
: 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.
3624
3617
Copying a reference is a "shallow" operation:
3625
3618
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 .
3629
3622
3630
3623
* Raw pointers (` * ` )
3631
3624
: Raw pointers are pointers without safety or liveness guarantees.
3632
3625
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.
3634
3627
Copying or dropping a raw pointer has no effect on the lifecycle of any
3635
3628
other value. Dereferencing a raw pointer or converting it to any other
3636
3629
pointer type is an [ ` unsafe ` operation] ( #unsafe-functions ) .
@@ -3663,38 +3656,26 @@ x = bo(5,7);
3663
3656
3664
3657
### Closure types
3665
3658
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.
3679
3661
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:
3682
3664
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.
3684
3668
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 ` .
3689
3673
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 ` .
3694
3678
3695
- call_closure (closure_no_args , closure_args );
3696
-
3697
- ```
3698
3679
3699
3680
### Trait objects
3700
3681
@@ -3741,19 +3722,19 @@ Within the body of an item that has type parameter declarations, the names of
3741
3722
its type parameters are types:
3742
3723
3743
3724
``` 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 > {
3745
3726
if xs.is_empty() {
3746
3727
return vec![];
3747
3728
}
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..] );
3750
3731
rest.insert(0, first);
3751
- return rest;
3732
+ rest
3752
3733
}
3753
3734
```
3754
3735
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 ` .
3757
3738
3758
3739
### Self types
3759
3740
0 commit comments