summaryrefslogtreecommitdiffstats
path: root/src/doc/rust-by-example
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-30 03:57:31 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-30 03:57:31 +0000
commitdc0db358abe19481e475e10c32149b53370f1a1c (patch)
treeab8ce99c4b255ce46f99ef402c27916055b899ee /src/doc/rust-by-example
parentReleasing progress-linux version 1.71.1+dfsg1-2~progress7.99u1. (diff)
downloadrustc-dc0db358abe19481e475e10c32149b53370f1a1c.tar.xz
rustc-dc0db358abe19481e475e10c32149b53370f1a1c.zip
Merging upstream version 1.72.1+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/doc/rust-by-example')
-rw-r--r--src/doc/rust-by-example/CONTRIBUTING.md4
-rw-r--r--src/doc/rust-by-example/src/conversion/from_into.md8
-rw-r--r--src/doc/rust-by-example/src/conversion/string.md2
-rw-r--r--src/doc/rust-by-example/src/error/option_unwrap/and_then.md15
-rw-r--r--src/doc/rust-by-example/src/flow_control/let_else.md7
-rw-r--r--src/doc/rust-by-example/src/flow_control/match/destructuring/destructure_structures.md6
-rw-r--r--src/doc/rust-by-example/src/fn/closures/closure_examples/iter_find.md4
-rw-r--r--src/doc/rust-by-example/src/hello/print.md2
-rw-r--r--src/doc/rust-by-example/src/types/cast.md2
-rw-r--r--src/doc/rust-by-example/src/unsafe/asm.md5
10 files changed, 37 insertions, 18 deletions
diff --git a/src/doc/rust-by-example/CONTRIBUTING.md b/src/doc/rust-by-example/CONTRIBUTING.md
index 665a708c0..43324d2bd 100644
--- a/src/doc/rust-by-example/CONTRIBUTING.md
+++ b/src/doc/rust-by-example/CONTRIBUTING.md
@@ -44,7 +44,7 @@ We use the following labels:
## Development workflow
-To build RBE, [install Rust], and then:
+To build RBE, [install Rust](https://www.rust-lang.org/tools/install), and then:
```bash
$ git clone https://github.com/rust-lang/rust-by-example
@@ -56,7 +56,7 @@ $ mdbook build
[install Rust]: http://rust-lang.org/install.html
The files will be in the `book` directory at the top-level; `mdbook serve` will
-open the contents in your web browser.
+open the contents in your web browser ([localhost:3000](http://localhost:3000) by default).
To run the tests:
diff --git a/src/doc/rust-by-example/src/conversion/from_into.md b/src/doc/rust-by-example/src/conversion/from_into.md
index 47b327142..d927cdd78 100644
--- a/src/doc/rust-by-example/src/conversion/from_into.md
+++ b/src/doc/rust-by-example/src/conversion/from_into.md
@@ -51,16 +51,16 @@ convert into as the compiler is unable to determine this most of the time.
However this is a small trade-off considering we get the functionality for free.
```rust,editable
-use std::convert::From;
+use std::convert::Into;
#[derive(Debug)]
struct Number {
value: i32,
}
-impl From<i32> for Number {
- fn from(item: i32) -> Self {
- Number { value: item }
+impl Into<Number> for i32 {
+ fn into(self) -> Number {
+ Number { value: self }
}
}
diff --git a/src/doc/rust-by-example/src/conversion/string.md b/src/doc/rust-by-example/src/conversion/string.md
index 9a17a2ce6..edadaaa51 100644
--- a/src/doc/rust-by-example/src/conversion/string.md
+++ b/src/doc/rust-by-example/src/conversion/string.md
@@ -28,7 +28,7 @@ fn main() {
## Parsing a String
-One of the more common types to convert a string into is a number. The idiomatic
+One of the more common types to convert a string into a number. The idiomatic
approach to this is to use the [`parse`] function and either to arrange for
type inference or to specify the type to parse using the 'turbofish' syntax.
Both alternatives are shown in the following example.
diff --git a/src/doc/rust-by-example/src/error/option_unwrap/and_then.md b/src/doc/rust-by-example/src/error/option_unwrap/and_then.md
index 42a1f3ec0..78c094015 100644
--- a/src/doc/rust-by-example/src/error/option_unwrap/and_then.md
+++ b/src/doc/rust-by-example/src/error/option_unwrap/and_then.md
@@ -8,7 +8,7 @@ known in some languages as flatmap, comes in.
`and_then()` calls its function input with the wrapped value and returns the result. If the `Option` is `None`, then it returns `None` instead.
-In the following example, `cookable_v2()` results in an `Option<Food>`.
+In the following example, `cookable_v3()` results in an `Option<Food>`.
Using `map()` instead of `and_then()` would have given an
`Option<Option<Food>>`, which is an invalid type for `eat()`.
@@ -44,12 +44,18 @@ fn cookable_v1(food: Food) -> Option<Food> {
}
// This can conveniently be rewritten more compactly with `and_then()`:
-fn cookable_v2(food: Food) -> Option<Food> {
+fn cookable_v3(food: Food) -> Option<Food> {
have_recipe(food).and_then(have_ingredients)
}
+// Otherwise we'd need to `flatten()` an `Option<Option<Food>>`
+// to get an `Option<Food>`:
+fn cookable_v2(food: Food) -> Option<Food> {
+ have_recipe(food).map(have_ingredients).flatten()
+}
+
fn eat(food: Food, day: Day) {
- match cookable_v2(food) {
+ match cookable_v3(food) {
Some(food) => println!("Yay! On {:?} we get to eat {:?}.", day, food),
None => println!("Oh no. We don't get to eat on {:?}?", day),
}
@@ -66,8 +72,9 @@ fn main() {
### See also:
-[closures][closures], [`Option`][option], and [`Option::and_then()`][and_then]
+[closures][closures], [`Option`][option], [`Option::and_then()`][and_then], and [`Option::flatten()`][flatten]
[closures]: ../../fn/closures.md
[option]: https://doc.rust-lang.org/std/option/enum.Option.html
[and_then]: https://doc.rust-lang.org/std/option/enum.Option.html#method.and_then
+[flatten]: https://doc.rust-lang.org/std/option/enum.Option.html#method.flatten
diff --git a/src/doc/rust-by-example/src/flow_control/let_else.md b/src/doc/rust-by-example/src/flow_control/let_else.md
index bc21723c7..bf1a53bde 100644
--- a/src/doc/rust-by-example/src/flow_control/let_else.md
+++ b/src/doc/rust-by-example/src/flow_control/let_else.md
@@ -2,6 +2,9 @@
> 🛈 stable since: rust 1.65
+>
+> 🛈 you can target specific edition by compiling like this
+> `rustc --edition=2021 main.rs`
With `let`-`else`, a refutable pattern can match and bind variables
@@ -22,7 +25,9 @@ fn get_count_item(s: &str) -> (u64, &str) {
(count, item)
}
-assert_eq!(get_count_item("3 chairs"), (3, "chairs"));
+fn main() {
+ assert_eq!(get_count_item("3 chairs"), (3, "chairs"));
+}
```
The scope of name bindings is the main thing that makes this different from
diff --git a/src/doc/rust-by-example/src/flow_control/match/destructuring/destructure_structures.md b/src/doc/rust-by-example/src/flow_control/match/destructuring/destructure_structures.md
index 9e43b70c1..252104fef 100644
--- a/src/doc/rust-by-example/src/flow_control/match/destructuring/destructure_structures.md
+++ b/src/doc/rust-by-example/src/flow_control/match/destructuring/destructure_structures.md
@@ -24,6 +24,12 @@ fn main() {
// this will give an error: pattern does not mention field `x`
//Foo { y } => println!("y = {}", y),
}
+
+ let faa = Foo { x: (1, 2), y: 3 };
+
+ // You do not need a match block to destructure structs:
+ let Foo { x : x0, y: y0 } = faa;
+ println!("Outside: x0 = {x0:?}, y0 = {y0}");
}
```
diff --git a/src/doc/rust-by-example/src/fn/closures/closure_examples/iter_find.md b/src/doc/rust-by-example/src/fn/closures/closure_examples/iter_find.md
index 0a79f7c9d..c3605d784 100644
--- a/src/doc/rust-by-example/src/fn/closures/closure_examples/iter_find.md
+++ b/src/doc/rust-by-example/src/fn/closures/closure_examples/iter_find.md
@@ -39,9 +39,9 @@ fn main() {
let array1 = [1, 2, 3];
let array2 = [4, 5, 6];
- // `iter()` for arrays yields `&i32`
+ // `iter()` for arrays yields `&&i32`
println!("Find 2 in array1: {:?}", array1.iter() .find(|&&x| x == 2));
- // `into_iter()` for arrays yields `i32`
+ // `into_iter()` for arrays yields `&i32`
println!("Find 2 in array2: {:?}", array2.into_iter().find(|&x| x == 2));
}
```
diff --git a/src/doc/rust-by-example/src/hello/print.md b/src/doc/rust-by-example/src/hello/print.md
index bf23a9023..ef25dab0e 100644
--- a/src/doc/rust-by-example/src/hello/print.md
+++ b/src/doc/rust-by-example/src/hello/print.md
@@ -88,7 +88,7 @@ for these types. To print text for custom types, more steps are required.
Implementing the `fmt::Display` trait automatically implements the
[`ToString`] trait which allows us to [convert] the type to [`String`][string].
-In *line 46*, `#[allow(dead_code)]` is an [attribute] which only apply to the module after it.
+In *line 43*, `#[allow(dead_code)]` is an [attribute] which only apply to the module after it.
### Activities
diff --git a/src/doc/rust-by-example/src/types/cast.md b/src/doc/rust-by-example/src/types/cast.md
index 3078d82c1..7e944d04c 100644
--- a/src/doc/rust-by-example/src/types/cast.md
+++ b/src/doc/rust-by-example/src/types/cast.md
@@ -53,7 +53,7 @@ fn main() {
// Unless it already fits, of course.
println!(" 128 as a i16 is: {}", 128 as i16);
- // 128 as u8 -> 128, whose value in 8-bit two's complement representation is:
+ // In boundary case 128 value in 8-bit two's complement representation is -128
println!(" 128 as a i8 is : {}", 128 as i8);
// repeating the example above
diff --git a/src/doc/rust-by-example/src/unsafe/asm.md b/src/doc/rust-by-example/src/unsafe/asm.md
index 1a3fab904..cbe52c840 100644
--- a/src/doc/rust-by-example/src/unsafe/asm.md
+++ b/src/doc/rust-by-example/src/unsafe/asm.md
@@ -254,7 +254,8 @@ fn main() {
// String is stored as ascii in ebx, edx, ecx in order
// Because ebx is reserved, the asm needs to preserve the value of it.
// So we push and pop it around the main asm.
- // (in 64 bit mode for 64 bit processors, 32 bit processors would use ebx)
+ // 64 bit mode on 64 bit processors does not allow pushing/popping of
+ // 32 bit registers (like ebx), so we have to use the extended rbx register instead.
unsafe {
asm!(
@@ -474,4 +475,4 @@ Options can be provided as an optional final argument to the `asm!` macro. We sp
These allow the compiler to better optimize code using `asm!`, for example by eliminating pure `asm!` blocks whose outputs are not needed.
-See the [reference](../../reference/inline-assembly.html) for the full list of available options and their effects.
+See the [reference](https://doc.rust-lang.org/stable/reference/inline-assembly.html) for the full list of available options and their effects.