]> git.lizzy.rs Git - rust.git/commitdiff
Auto merge of #24744 - bluss:reference-1, r=steveklabnik
authorbors <bors@rust-lang.org>
Fri, 24 Apr 2015 15:48:35 +0000 (15:48 +0000)
committerbors <bors@rust-lang.org>
Fri, 24 Apr 2015 15:48:35 +0000 (15:48 +0000)
Audit & Edit Chapter 8.1 Types in reference manual

- Remove mention of unit type
- Update closure types and reference types sections
- Fix minor typos

1  2 
src/doc/reference.md

index 1cedbf299c327949f370b08dc2bda99e5467591b,3de23250547767837725e286e4629502c0f30855..f5a4f12e5faee97502d968917152d3112070beaa
@@@ -3479,9 -3425,9 +3472,9 @@@ is not a surrogate), represented as a 3
  UTF-32 string.
  
  A value of type `str` is a Unicode string, represented as an array of 8-bit
 -unsigned bytes holding a sequence of UTF-8 codepoints. Since `str` is of
 +unsigned bytes holding a sequence of UTF-8 code points. Since `str` is of
  unknown size, it is not a _first-class_ type, but can only be instantiated
- through a pointer type, such as `&str` or `String`.
+ through a pointer type, such as `&str`.
  
  ### Tuple types
  
@@@ -3663,43 -3609,31 +3656,31 @@@ x = bo(5,7)
  
  ### Closure types
  
- ```{.ebnf .notation}
- closure_type := [ 'unsafe' ] [ '<' lifetime-list '>' ] '|' arg-list '|'
-                 [ ':' bound-list ] [ '->' type ]
- lifetime-list := lifetime | lifetime ',' lifetime-list
- arg-list := ident ':' type | ident ':' type ',' arg-list
- bound-list := bound | bound '+' bound-list
- bound := path | lifetime
- ```
- The type of a closure mapping an input of type `A` to an output of type `B` is
- `|A| -> B`. A closure with no arguments or return values has type `||`.
- An example of creating and calling a closure:
+ A [lambda expression](#lambda-expressions) produces a closure value with
+ a unique, anonymous type that cannot be written out.
  
- ```rust
- let captured_var = 10;
+ Depending on the requirements of the closure, its type implements one or
+ more of the closure traits:
  
- let closure_no_args = || println!("captured_var={}", captured_var);
+ * `FnOnce`
+   : The closure can be called once. A closure called as `FnOnce`
+     can move out values from its environment.
  
- let closure_args = |arg: i32| -> i32 {
-   println!("captured_var={}, arg={}", captured_var, arg);
-   arg // Note lack of semicolon after 'arg'
- };
+ * `FnMut`
+   : The closure can be called multiple times as mutable. A closure called as
+     `FnMut` can mutate values from its environment. `FnMut` implies
+     `FnOnce`.
  
- fn call_closure<F: Fn(), G: Fn(i32) -> i32>(c1: F, c2: G) {
-   c1();
-   c2(2);
- }
+ * `Fn`
+   : The closure can be called multiple times through a shared reference.
+     A closure called as `Fn` can neither move out from nor mutate values
+     from its environment. `Fn` implies `FnMut` and `FnOnce`.
  
- call_closure(closure_no_args, closure_args);
- ```
  
 -### Object types
 +### Trait objects
  
  Every trait item (see [traits](#traits)) defines a type with the same name as
 -the trait. This type is called the _object type_ of the trait. Object types
 +the trait. This type is called the _trait object_ of the trait. Trait objects
  permit "late binding" of methods, dispatched using _virtual method tables_
  ("vtables"). Whereas most calls to trait methods are "early bound" (statically
  resolved) to specific implementations at compile time, a call to a method on an