@@ -1750,6 +1750,24 @@ closures, but they also own them: that is, no other code can access
1750
1750
them. Owned closures are used in concurrent code, particularly
1751
1751
for spawning [tasks][tasks].
1752
1752
1753
+ Closures can be used to spawn tasks.
1754
+ A practical example of this pattern is found when using the `spawn` function,
1755
+ which starts a new task.
1756
+
1757
+ ~~~~
1758
+ use std::task::spawn;
1759
+
1760
+ // proc is the closure which will be spawned.
1761
+ spawn(proc() {
1762
+ debug!("I'm a new task")
1763
+ });
1764
+ ~~~~
1765
+
1766
+ > ***Note:*** If you want to see the output of `debug!` statements, you will need to turn on
1767
+ > `debug!` logging. To enable `debug!` logging, set the RUST_LOG environment
1768
+ > variable to the name of your crate, which, for a file named `foo.rs`, will be
1769
+ > `foo` (e.g., with bash, `export RUST_LOG=foo`).
1770
+
1753
1771
## Closure compatibility
1754
1772
1755
1773
Rust closures have a convenient subtyping property: you can pass any kind of
@@ -1771,45 +1789,6 @@ call_twice(function);
1771
1789
> in small ways. At the moment they can be unsound in some
1772
1790
> scenarios, particularly with non-copyable types.
1773
1791
1774
- ## Do syntax
1775
-
1776
- The `do` expression makes it easier to call functions that take procedures
1777
- as arguments.
1778
-
1779
- Consider this function that takes a procedure:
1780
-
1781
- ~~~~
1782
- fn call_it(op: proc(v: int)) {
1783
- op(10)
1784
- }
1785
- ~~~~
1786
-
1787
- As a caller, if we use a closure to provide the final operator
1788
- argument, we can write it in a way that has a pleasant, block-like
1789
- structure.
1790
-
1791
- ~~~~
1792
- # fn call_it(op: proc(v: int)) { }
1793
- call_it(proc(n) {
1794
- println!("{}", n);
1795
- });
1796
- ~~~~
1797
-
1798
- A practical example of this pattern is found when using the `spawn` function,
1799
- which starts a new task.
1800
-
1801
- ~~~~
1802
- use std::task::spawn;
1803
- spawn(proc() {
1804
- debug!("I'm a new task")
1805
- });
1806
- ~~~~
1807
-
1808
- If you want to see the output of `debug!` statements, you will need to turn on
1809
- `debug!` logging. To enable `debug!` logging, set the RUST_LOG environment
1810
- variable to the name of your crate, which, for a file named `foo.rs`, will be
1811
- `foo` (e.g., with bash, `export RUST_LOG=foo`).
1812
-
1813
1792
# Methods
1814
1793
1815
1794
Methods are like functions except that they always begin with a special argument,
0 commit comments