Skip to content

Commit 93cd8da

Browse files
committed
---
yaml --- r: 128635 b: refs/heads/try c: 87d2bf4 h: refs/heads/master i: 128633: 1a7f678 128631: 12397a0 v: v3
1 parent dd6991a commit 93cd8da

File tree

176 files changed

+2091
-1556
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

176 files changed

+2091
-1556
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
refs/heads/master: 07d86b46a949a94223da714e35b343243e4ecce4
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: a86d9ad15e339ab343a12513f9c90556f677b9ca
5-
refs/heads/try: dac73ad3c1b92f51cade8dbac4879ffb951ded3f
5+
refs/heads/try: 87d2bf400ce1bc2cec832f9d5b8e763f06bb7f43
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: 147ecfdd8221e4a4d4e090486829a06da1e0ca3c

branches/try/mk/platform.mk

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ RUSTC_CROSS_FLAGS_arm-unknown-linux-gnueabi :=
377377
# mipsel-linux configuration
378378
CC_mipsel-linux=mipsel-linux-gcc
379379
CXX_mipsel-linux=mipsel-linux-g++
380-
CPP_mipsel-linux=mipsel-linux-gcc
380+
CPP_mipsel-linux=mipsel-linux-gcc
381381
AR_mipsel-linux=mipsel-linux-ar
382382
CFG_LIB_NAME_mipsel-linux=lib$(1).so
383383
CFG_STATIC_LIB_NAME_mipsel-linux=lib$(1).a
@@ -641,7 +641,7 @@ define CFG_MAKE_TOOLCHAIN
641641
CXX_$(1)=$(CROSS_PREFIX_$(1))$(CXX_$(1))
642642
CPP_$(1)=$(CROSS_PREFIX_$(1))$(CPP_$(1))
643643
AR_$(1)=$(CROSS_PREFIX_$(1))$(AR_$(1))
644-
RUSTC_CROSS_FLAGS_$(1)=-C linker=$$(call FIND_COMPILER,$$(CXX_$(1))) \
644+
RUSTC_CROSS_FLAGS_$(1)=-C linker=$$(call FIND_COMPILER,$$(CC_$(1))) \
645645
-C ar=$$(call FIND_COMPILER,$$(AR_$(1))) $(RUSTC_CROSS_FLAGS_$(1))
646646

647647
RUSTC_FLAGS_$(1)=$$(RUSTC_CROSS_FLAGS_$(1)) $(RUSTC_FLAGS_$(1))

branches/try/src/doc/guide.md

Lines changed: 222 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1647,14 +1647,14 @@ $ cargo build
16471647
$
16481648
```
16491649

1650-
Excellent! Open up your `src/guessing_game.rs` again. We'll be writing all of
1650+
Excellent! Open up your `src/main.rs` again. We'll be writing all of
16511651
our code in this file. We'll talk about multiple-file projects later on in the
16521652
guide.
16531653

16541654
## Processing a Guess
16551655

16561656
Let's get to it! The first thing we need to do for our guessing game is
1657-
allow our player to input a guess. Put this in your `src/guessing_game.rs`:
1657+
allow our player to input a guess. Put this in your `src/main.rs`:
16581658

16591659
```{rust,no_run}
16601660
use std::io;
@@ -1734,9 +1734,9 @@ this using `cargo build`:
17341734
```{notrust,no_run}
17351735
$ cargo build
17361736
Compiling guessing_game v0.1.0 (file:/home/you/projects/guessing_game)
1737-
src/guessing_game.rs:7:26: 7:34 error: the type of this value must be known in this context
1738-
src/guessing_game.rs:7 let secret_number = (rand::random() % 100i) + 1i;
1739-
^~~~~~~~
1737+
src/main.rs:7:26: 7:34 error: the type of this value must be known in this context
1738+
src/main.rs:7 let secret_number = (rand::random() % 100i) + 1i;
1739+
^~~~~~~~
17401740
error: aborting due to previous error
17411741
```
17421742

@@ -1896,12 +1896,12 @@ If we try to compile, we'll get some errors:
18961896
```{notrust,ignore}
18971897
$ cargo build
18981898
Compiling guessing_game v0.1.0 (file:/home/you/projects/guessing_game)
1899-
src/guessing_game.rs:20:15: 20:20 error: mismatched types: expected `int` but found `collections::string::String` (expected int but found struct collections::string::String)
1900-
src/guessing_game.rs:20 match cmp(input, secret_number) {
1901-
^~~~~
1902-
src/guessing_game.rs:20:22: 20:35 error: mismatched types: expected `int` but found `uint` (expected int but found uint)
1903-
src/guessing_game.rs:20 match cmp(input, secret_number) {
1904-
^~~~~~~~~~~~~
1899+
src/main.rs:20:15: 20:20 error: mismatched types: expected `int` but found `collections::string::String` (expected int but found struct collections::string::String)
1900+
src/main.rs:20 match cmp(input, secret_number) {
1901+
^~~~~
1902+
src/main.rs:20:22: 20:35 error: mismatched types: expected `int` but found `uint` (expected int but found uint)
1903+
src/main.rs:20 match cmp(input, secret_number) {
1904+
^~~~~~~~~~~~~
19051905
error: aborting due to 2 previous errors
19061906
```
19071907

@@ -1950,9 +1950,9 @@ And try compiling again:
19501950
```{notrust,ignore}
19511951
$ cargo build
19521952
Compiling guessing_game v0.1.0 (file:/home/you/projects/guessing_game)
1953-
src/guessing_game.rs:20:15: 20:20 error: mismatched types: expected `uint` but found `collections::string::String` (expected uint but found struct collections::string::String)
1954-
src/guessing_game.rs:20 match cmp(input, secret_number) {
1955-
^~~~~
1953+
src/main.rs:20:15: 20:20 error: mismatched types: expected `uint` but found `collections::string::String` (expected uint but found struct collections::string::String)
1954+
src/main.rs:20 match cmp(input, secret_number) {
1955+
^~~~~
19561956
error: aborting due to previous error
19571957
```
19581958

@@ -2053,9 +2053,9 @@ Let's try it out!
20532053
```{notrust,ignore}
20542054
$ cargo build
20552055
Compiling guessing_game v0.1.0 (file:/home/you/projects/guessing_game)
2056-
src/guessing_game.rs:22:15: 22:24 error: mismatched types: expected `uint` but found `core::option::Option<uint>` (expected uint but found enum core::option::Option)
2057-
src/guessing_game.rs:22 match cmp(input_num, secret_number) {
2058-
^~~~~~~~~
2056+
src/main.rs:22:15: 22:24 error: mismatched types: expected `uint` but found `core::option::Option<uint>` (expected uint but found enum core::option::Option)
2057+
src/main.rs:22 match cmp(input_num, secret_number) {
2058+
^~~~~~~~~
20592059
error: aborting due to previous error
20602060
```
20612061

@@ -3295,7 +3295,7 @@ fn times_four(x: int) -> int { x * 4 }
32953295
#[cfg(test)]
32963296
mod test {
32973297
use super::add_three;
3298-
use super::add_four;
3298+
use super::times_four;
32993299
33003300
#[test]
33013301
fn test_add_three() {
@@ -3344,7 +3344,7 @@ about yet, and that's these lines:
33443344

33453345
```{rust,ignore}
33463346
use super::add_three;
3347-
use super::add_four;
3347+
use super::times_four;
33483348
```
33493349

33503350
Because we've made a nested module, we can import functions from the parent
@@ -3614,7 +3614,209 @@ guide](http://doc.rust-lang.org/guide-pointers.html#rc-and-arc).
36143614

36153615
# Patterns
36163616

3617-
# Lambdas
3617+
# Closures
3618+
3619+
So far, we've made lots of functions in Rust. But we've given them all names.
3620+
Rust also allows us to create anonymous functions too. Rust's anonymous
3621+
functions are called **closure**s. By themselves, closures aren't all that
3622+
interesting, but when you combine them with functions that take closures as
3623+
arguments, really powerful things are possible.
3624+
3625+
Let's make a closure:
3626+
3627+
```{rust}
3628+
let add_one = |x| { 1i + x };
3629+
3630+
println!("The 5 plus 1 is {}.", add_one(5i));
3631+
```
3632+
3633+
We create a closure using the `|...| { ... }` syntax, and then we create a
3634+
binding so we can use it later. Note that we call the function using the
3635+
binding name and two parentheses, just like we would for a named function.
3636+
3637+
Let's compare syntax. The two are pretty close:
3638+
3639+
```{rust}
3640+
let add_one = |x: int| -> int { 1i + x };
3641+
fn add_one (x: int) -> int { 1i + x }
3642+
```
3643+
3644+
As you may have noticed, closures infer their argument and return types, so you
3645+
don't need to declare one. This is different from named functions, which
3646+
default to returning unit (`()`).
3647+
3648+
There's one big difference between a closure and named functions, and it's in
3649+
the name: a function "closes over its environment." What's that mean? It means
3650+
this:
3651+
3652+
```{rust}
3653+
fn main() {
3654+
let x = 5i;
3655+
3656+
let printer = || { println!("x is: {}", x); };
3657+
3658+
printer(); // prints "x is: 5"
3659+
}
3660+
```
3661+
3662+
The `||` syntax means this is an anonymous closure that takes no arguments.
3663+
Without it, we'd just have a block of code in `{}`s.
3664+
3665+
In other words, a closure has access to variables in the scope that it's
3666+
defined. The closure borrows any variables that it uses. This will error:
3667+
3668+
```{rust,ignore}
3669+
fn main() {
3670+
let mut x = 5i;
3671+
3672+
let printer = || { println!("x is: {}", x); };
3673+
3674+
x = 6i; // error: cannot assign to `x` because it is borrowed
3675+
}
3676+
```
3677+
3678+
## Procs
3679+
3680+
Rust has a second type of closure, called a **proc**. Procs are created
3681+
with the `proc` keyword:
3682+
3683+
```{rust}
3684+
let x = 5i;
3685+
3686+
let p = proc() { x * x };
3687+
println!("{}", p()); // prints 25
3688+
```
3689+
3690+
Procs have a big difference from closures: they may only be called once. This
3691+
will error when we try to compile:
3692+
3693+
```{rust,ignore}
3694+
let x = 5i;
3695+
3696+
let p = proc() { x * x };
3697+
println!("{}", p());
3698+
println!("{}", p()); // error: use of moved value `p`
3699+
```
3700+
3701+
This restriction is important. Procs are allowed to consume values that they
3702+
capture, and thus have to be restricted to being called once for soundness
3703+
reasons: any value consumed would be invalid on a second call.
3704+
3705+
Procs are most useful with Rust's concurrency features, and so we'll just leave
3706+
it at this for now. We'll talk about them more in the "Tasks" section of the
3707+
guide.
3708+
3709+
## Accepting closures as arguments
3710+
3711+
Closures are most useful as an argument to another function. Here's an example:
3712+
3713+
```{rust}
3714+
fn twice(x: int, f: |int| -> int) -> int {
3715+
f(x) + f(x)
3716+
}
3717+
3718+
fn main() {
3719+
let square = |x: int| { x * x };
3720+
3721+
twice(5i, square); // evaluates to 50
3722+
}
3723+
```
3724+
3725+
Let's break example down, starting with `main`:
3726+
3727+
```{rust}
3728+
let square = |x: int| { x * x };
3729+
```
3730+
3731+
We've seen this before. We make a closure that takes an integer, and returns
3732+
its square.
3733+
3734+
```{rust,ignore}
3735+
twice(5i, square); // evaluates to 50
3736+
```
3737+
3738+
This line is more interesting. Here, we call our function, `twice`, and we pass
3739+
it two arguments: an integer, `5`, and our closure, `square`. This is just like
3740+
passing any other two variable bindings to a function, but if you've never
3741+
worked with closures before, it can seem a little complex. Just think: "I'm
3742+
passing two variables, one is an int, and one is a function."
3743+
3744+
Next, let's look at how `twice` is defined:
3745+
3746+
```{rust,ignore}
3747+
fn twice(x: int, f: |int| -> int) -> int {
3748+
```
3749+
3750+
`twice` takes two arguments, `x` and `f`. That's why we called it with two
3751+
arguments. `x` is an `int`, we've done that a ton of times. `f` is a function,
3752+
though, and that function takes an `int` and returns an `int`. Notice
3753+
how the `|int| -> int` syntax looks a lot like our definition of `square`
3754+
above, if we added the return type in:
3755+
3756+
```{rust}
3757+
let square = |x: int| -> int { x * x };
3758+
// |int| -> int
3759+
```
3760+
3761+
This function takes an `int` and returns an `int`.
3762+
3763+
This is the most complicated function signature we've seen yet! Give it a read
3764+
a few times until you can see how it works. It takes a teeny bit of practice, and
3765+
then it's easy.
3766+
3767+
Finally, `twice` returns an `int` as well.
3768+
3769+
Okay, let's look at the body of `twice`:
3770+
3771+
```{rust}
3772+
fn twice(x: int, f: |int| -> int) -> int {
3773+
f(x) + f(x)
3774+
}
3775+
```
3776+
3777+
Since our closure is named `f`, we can call it just like we called our closures
3778+
before. And we pass in our `x` argument to each one. Hence 'twice.'
3779+
3780+
If you do the math, `(5 * 5) + (5 * 5) == 50`, so that's the output we get.
3781+
3782+
Play around with this concept until you're comfortable with it. Rust's standard
3783+
library uses lots of closures, where appropriate, so you'll be using
3784+
this technique a lot.
3785+
3786+
If we didn't want to give `square` a name, we could also just define it inline.
3787+
This example is the same as the previous one:
3788+
3789+
```{rust}
3790+
fn twice(x: int, f: |int| -> int) -> int {
3791+
f(x) + f(x)
3792+
}
3793+
3794+
fn main() {
3795+
twice(5i, |x: int| { x * x }); // evaluates to 50
3796+
}
3797+
```
3798+
3799+
A named function's name can be used wherever you'd use a closure. Another
3800+
way of writing the previous example:
3801+
3802+
```{rust}
3803+
fn twice(x: int, f: |int| -> int) -> int {
3804+
f(x) + f(x)
3805+
}
3806+
3807+
fn square(x: int) -> int { x * x }
3808+
3809+
fn main() {
3810+
twice(5i, square); // evaluates to 50
3811+
}
3812+
```
3813+
3814+
Doing this is not particularly common, but every once in a while, it's useful.
3815+
3816+
That's all you need to get the hang of closures! Closures are a little bit
3817+
strange at first, but once you're used to using them, you'll miss them in any
3818+
language that doesn't have them. Passing functions to other functions is
3819+
incredibly powerful. Next, let's look at one of those things: iterators.
36183820

36193821
# iterators
36203822

branches/try/src/doc/intro.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -359,10 +359,11 @@ fn main() {
359359
// Take the lock, along with exclusive access to the underlying array
360360
let mut numbers = numbers_lock.lock();
361361
362-
// This is ugly for now, but will be replaced by
363-
// `numbers[num as uint] += 1` in the near future.
362+
// This is ugly for now because of the need for `get_mut`, but
363+
// will be replaced by `numbers[num as uint] += 1`
364+
// in the near future.
364365
// See: https://github.com/rust-lang/rust/issues/6515
365-
*numbers.get_mut(num as uint) = *numbers.get_mut(num as uint) + 1;
366+
*numbers.get_mut(num as uint) += 1;
366367
367368
println!("{}", (*numbers)[num as uint]);
368369

branches/try/src/doc/rust.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,7 @@ production. See [tokens](#tokens) for more information.
112112

113113
## Input format
114114

115-
Rust input is interpreted as a sequence of Unicode codepoints encoded in UTF-8,
116-
normalized to Unicode normalization form NFKC.
115+
Rust input is interpreted as a sequence of Unicode codepoints encoded in UTF-8.
117116
Most Rust grammar rules are defined in terms of printable ASCII-range codepoints,
118117
but a small number are defined in terms of Unicode properties or explicit
119118
codepoint lists. [^inputformat]

branches/try/src/doc/tutorial.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ OS X, the install page provides links to native installers.
7676
> the precise details of which are not discussed here.
7777
7878
For Linux and OS X, the install page provides links to binary tarballs.
79-
To install the Rust compiler from the from a binary tarball, download
79+
To install the Rust compiler from a binary tarball, download
8080
the binary package, extract it, and execute the `install.sh` script in
8181
the root directory of the package.
8282

@@ -2196,7 +2196,7 @@ and may not be overridden:
21962196
Types are sendable
21972197
unless they contain references.
21982198

2199-
* `Share` - Types that are *threadsafe*
2199+
* `Share` - Types that are *threadsafe*.
22002200
These are types that are safe to be used across several threads with access to
22012201
a `&T` pointer. `Mutex<T>` is an example of a *sharable* type with internal mutable data.
22022202

branches/try/src/etc/ctags.rust

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77
--regex-Rust=/^[ \t]*(pub[ \t]+)?mod[ \t]+([a-zA-Z0-9_]+)/\2/m,modules,module names/
88
--regex-Rust=/^[ \t]*(pub[ \t]+)?static[ \t]+([a-zA-Z0-9_]+)/\2/c,consts,static constants/
99
--regex-Rust=/^[ \t]*(pub[ \t]+)?trait[ \t]+([a-zA-Z0-9_]+)/\2/t,traits,traits/
10-
--regex-Rust=/^[ \t]*(pub[ \t]+)?impl([ \t\n]+<.*>)?[ \t]+([a-zA-Z0-9_]+)/\3/i,impls,trait implementations/
10+
--regex-Rust=/^[ \t]*(pub[ \t]+)?impl([ \t\n]*<[^>]*>)?[ \t]+(([a-zA-Z0-9_:]+)[ \t]*(<[^>]*>)?[ \t]+(for)[ \t]+)?([a-zA-Z0-9_]+)/\4 \6 \7/i,impls,trait implementations/
1111
--regex-Rust=/^[ \t]*macro_rules![ \t]+([a-zA-Z0-9_]+)/\1/d,macros,macro definitions/

0 commit comments

Comments
 (0)