Skip to content

Commit e60b98d

Browse files
authored
Merge pull request #1 from rust-lang/master
Update fork
2 parents 67cfbf3 + a0164b7 commit e60b98d

File tree

9 files changed

+86
-39
lines changed

9 files changed

+86
-39
lines changed

src/SUMMARY.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
- [As output parameters](fn/closures/output_parameters.md)
7070
- [Examples in `std`](fn/closures/closure_examples.md)
7171
- [Iterator::any](fn/closures/closure_examples/iter_any.md)
72-
- [Iterator::find](fn/closures/closure_examples/iter_find.md)
72+
- [Searching through iterators](fn/closures/closure_examples/iter_find.md)
7373
- [Higher Order Functions](fn/hof.md)
7474
- [Diverging functions](fn/diverging.md)
7575

@@ -139,8 +139,8 @@
139139
- [Iterators](trait/iter.md)
140140
- [`impl Trait`](trait/impl_trait.md)
141141
- [Clone](trait/clone.md)
142-
- [Supertraits](traits/supertraits.md)
143-
- [Disambiguating overlapping traits](traits/disambiguating.md)
142+
- [Supertraits](trait/supertraits.md)
143+
- [Disambiguating overlapping traits](trait/disambiguating.md)
144144

145145
- [macro_rules!](macros.md)
146146
- [Syntax](macros/syntax.md)

src/conversion/string.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ trait is implemented for that type. This is implemented for numerous types
3838
within the standard library. To obtain this functionality on a user defined type
3939
simply implement the [`FromStr`] trait for that type.
4040

41-
```rust
41+
```rust,editable
4242
fn main() {
4343
let parsed: i32 = "5".parse().unwrap();
4444
let turbo_parsed = "10".parse::<i32>().unwrap();

src/custom_types/enum.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ fn main() {
5555

5656
## Type aliases
5757

58-
If you use a type alias, you can refer to each enum variant via its alias.
58+
If you use a type alias, you can refer to each enum variant via its alias.
5959
This might be useful if the enum's name is too long or too generic, and you
6060
want to rename it.
6161

@@ -93,16 +93,17 @@ impl VeryVerboseEnumOfThingsToDoWithNumbers {
9393
}
9494
```
9595

96-
To learn more about enums and type aliases, you can read the
96+
To learn more about enums and type aliases, you can read the
9797
[stabilization report][aliasreport] from when this feature was stabilized into
98-
Rust.
98+
Rust.
9999

100100
### See also:
101101

102-
[`match`][match], [`fn`][fn], and [`String`][str], []
102+
[`match`][match], [`fn`][fn], and [`String`][str], ["Type alias enum variants" RFC][type_alias_rfc]
103103

104104
[c_struct]: https://en.wikipedia.org/wiki/Struct_(C_programming_language)
105105
[match]: ../flow_control/match.md
106106
[fn]: ../fn.md
107107
[str]: ../std/str.md
108108
[aliasreport]: https://github.com/rust-lang/rust/pull/61682/#issuecomment-502472847
109+
[type_alias_rfc]: https://rust-lang.github.io/rfcs/2338-type-alias-enum-variants.html

src/flow_control/match.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ fn main() {
1515
// Match several values
1616
2 | 3 | 5 | 7 | 11 => println!("This is a prime"),
1717
// Match an inclusive range
18-
13...19 => println!("A teen"),
18+
13..=19 => println!("A teen"),
1919
// Handle the rest of cases
2020
_ => println!("Ain't special"),
2121
}
@@ -31,4 +31,4 @@ fn main() {
3131
3232
println!("{} -> {}", boolean, binary);
3333
}
34-
```
34+
```

src/fn/closures/capture.md

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,37 +18,51 @@ fn main() {
1818
1919
let color = "green";
2020
21-
// A closure to print `color` which immediately borrows (`&`)
22-
// `color` and stores the borrow and closure in the `print`
23-
// variable. It will remain borrowed until `print` goes out of
24-
// scope. `println!` only requires `by reference` so it doesn't
21+
// A closure to print `color` which immediately borrows (`&`) `color` and
22+
// stores the borrow and closure in the `print` variable. It will remain
23+
// borrowed until `print` is used the last time.
24+
//
25+
// `println!` only requires arguments by immutable reference so it doesn't
2526
// impose anything more restrictive.
2627
let print = || println!("`color`: {}", color);
2728
2829
// Call the closure using the borrow.
2930
print();
31+
32+
// `color` can be borrowed immutably again, because the closure only holds
33+
// an immutable reference to `color`.
34+
let _reborrow = &color;
3035
print();
3136
32-
let mut count = 0;
37+
// A move or reborrow is allowed after the final use of `print`
38+
let _color_moved = color;
3339
34-
// A closure to increment `count` could take either `&mut count`
35-
// or `count` but `&mut count` is less restrictive so it takes
36-
// that. Immediately borrows `count`.
40+
41+
let mut count = 0;
42+
// A closure to increment `count` could take either `&mut count` or `count`
43+
// but `&mut count` is less restrictive so it takes that. Immediately
44+
// borrows `count`.
3745
//
38-
// A `mut` is required on `inc` because a `&mut` is stored inside.
39-
// Thus, calling the closure mutates the closure which requires
40-
// a `mut`.
46+
// A `mut` is required on `inc` because a `&mut` is stored inside. Thus,
47+
// calling the closure mutates the closure which requires a `mut`.
4148
let mut inc = || {
4249
count += 1;
4350
println!("`count`: {}", count);
4451
};
4552
46-
// Call the closure.
47-
inc();
53+
// Call the closure using a mutable borrow.
4854
inc();
4955
50-
//let _reborrow = &mut count;
56+
// The closure still mutably borrows `count` because it is called later.
57+
// An attempt to reborrow will lead to an error.
58+
// let _reborrow = &count;
5159
// ^ TODO: try uncommenting this line.
60+
inc();
61+
62+
// The closure no longer needs to borrow `&mut count`. Therefore, it is
63+
// possible to reborrow without an error
64+
let _count_reborrowed = &mut count;
65+
5266
5367
// A non-copy type.
5468
let movable = Box::new(3);
@@ -64,7 +78,7 @@ fn main() {
6478
6579
// `consume` consumes the variable so this can only be called once.
6680
consume();
67-
//consume();
81+
// consume();
6882
// ^ TODO: Try uncommenting this line.
6983
}
7084
```
@@ -82,7 +96,7 @@ fn main() {
8296
println!("{}", contains(&1));
8397
println!("{}", contains(&4));
8498
85-
// `println!("There're {} elements in vec", haystack.len());`
99+
// println!("There're {} elements in vec", haystack.len());
86100
// ^ Uncommenting above line will result in compile-time error
87101
// because borrow checker doesn't allow re-using variable after it
88102
// has been moved.

src/fn/closures/closure_examples/iter_find.md

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
# Iterator::find
1+
# Searching through iterators
22

3-
`Iterator::find` is a function which when passed an iterator, will return
4-
the first element which satisfies the predicate as an `Option`. Its
5-
signature:
3+
`Iterator::find` is a function which iterates over an iterator and searches for the
4+
first value which satisfies some condition. If none of the values satisfy the
5+
condition, it returns `None`. Its signature:
66

77
```rust,ignore
88
pub trait Iterator {
@@ -29,9 +29,11 @@ fn main() {
2929
// `into_iter()` for vecs yields `i32`.
3030
let mut into_iter = vec2.into_iter();
3131
32-
// A reference to what is yielded is `&&i32`. Destructure to `i32`.
32+
// `iter()` for vecs yields `&i32`, and we want to reference one of its
33+
// items, so we have to destructure `&&i32` to `i32`
3334
println!("Find 2 in vec1: {:?}", iter .find(|&&x| x == 2));
34-
// A reference to what is yielded is `&i32`. Destructure to `i32`.
35+
// `into_iter()` for vecs yields `i32`, and we want to reference one of
36+
// its items, so we have to destructure `&i32` to `i32`
3537
println!("Find 2 in vec2: {:?}", into_iter.find(| &x| x == 2));
3638
3739
let array1 = [1, 2, 3];
@@ -44,8 +46,33 @@ fn main() {
4446
}
4547
```
4648

49+
`Iterator::find` gives you a reference to the item. But if you want the _index_ of the
50+
item, use `Iterator::position`.
51+
52+
```rust,editable
53+
fn main() {
54+
let vec = vec![1, 9, 3, 3, 13, 2];
55+
56+
let index_of_first_even_number = vec.iter().position(|x| x % 2 == 0);
57+
assert_eq!(index_of_first_even_number, Some(5));
58+
59+
60+
let index_of_first_negative_number = vec.iter().position(|x| x < &0);
61+
assert_eq!(index_of_first_negative_number, None);
62+
}
63+
```
64+
4765
### See also:
4866

4967
[`std::iter::Iterator::find`][find]
5068

69+
[`std::iter::Iterator::find_map`][find_map]
70+
71+
[`std::iter::Iterator::position`][position]
72+
73+
[`std::iter::Iterator::rposition`][rposition]
74+
5175
[find]: https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.find
76+
[find_map]: https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.find_map
77+
[position]: https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.position
78+
[rposition]: https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.rposition

src/fn/closures/output_parameters.md

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@ output parameters should also be possible. However, anonymous
55
closure types are, by definition, unknown, so we have to use
66
`impl Trait` to return them.
77

8-
The valid traits for returns are slightly different than before:
8+
The valid traits for returning a closure are:
99

10-
* `Fn`: normal
11-
* `FnMut`: normal
12-
* `FnOnce`: There are some unusual things at play here, so the [`FnBox`][fnbox]
13-
type is currently needed, and is unstable. This is expected to change in
14-
the future.
10+
* `Fn`
11+
* `FnMut`
12+
* `FnOnce`
1513

1614
Beyond this, the `move` keyword must be used, which signals that all captures
1715
occur by value. This is required because any captures by reference would be
@@ -31,12 +29,20 @@ fn create_fnmut() -> impl FnMut() {
3129
move || println!("This is a: {}", text)
3230
}
3331
32+
fn create_fnonce() -> impl FnOnce() {
33+
let text = "FnOnce".to_owned();
34+
35+
move || println!("This is a: {}", text)
36+
}
37+
3438
fn main() {
3539
let fn_plain = create_fn();
3640
let mut fn_mut = create_fnmut();
41+
let fn_once = create_fnonce();
3742
3843
fn_plain();
3944
fn_mut();
45+
fn_once();
4046
}
4147
```
4248

@@ -46,6 +52,5 @@ fn main() {
4652

4753
[fn]: https://doc.rust-lang.org/std/ops/trait.Fn.html
4854
[fnmut]: https://doc.rust-lang.org/std/ops/trait.FnMut.html
49-
[fnbox]: https://doc.rust-lang.org/std/boxed/trait.FnBox.html
5055
[generics]: ../../generics.md
5156
[impltrait]: ../../trait/impl_trait.md
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)