From 4e8199b572f2035b7749cba276ece3a26630d23e Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 14:18:21 +0200 Subject: Adding upstream version 1.67.1+dfsg1. Signed-off-by: Daniel Baumann --- src/doc/rust-by-example/.github/workflows/rbe.yml | 2 +- src/doc/rust-by-example/src/crates/using_lib.md | 2 +- src/doc/rust-by-example/src/fn/closures.md | 22 +++++++------- .../src/fn/closures/input_parameters.md | 2 +- src/doc/rust-by-example/src/hello/print.md | 2 +- src/doc/rust-by-example/src/scope/lifetime.md | 2 +- .../src/std_misc/file/read_lines.md | 34 ++++++++++++++++++++++ src/doc/rust-by-example/src/types/cast.md | 20 ++++++------- src/doc/rust-by-example/triagebot.toml | 3 ++ 9 files changed, 64 insertions(+), 25 deletions(-) (limited to 'src/doc/rust-by-example') diff --git a/src/doc/rust-by-example/.github/workflows/rbe.yml b/src/doc/rust-by-example/.github/workflows/rbe.yml index 94b7cdc95..76b127b96 100644 --- a/src/doc/rust-by-example/.github/workflows/rbe.yml +++ b/src/doc/rust-by-example/.github/workflows/rbe.yml @@ -42,7 +42,7 @@ jobs: sh linkcheck.sh --all rust-by-example - name: Upload Artifact - uses: actions/upload-artifact@v1 + uses: actions/upload-artifact@v3 with: name: rust-by-example path: book diff --git a/src/doc/rust-by-example/src/crates/using_lib.md b/src/doc/rust-by-example/src/crates/using_lib.md index 102080700..8bd0feb94 100644 --- a/src/doc/rust-by-example/src/crates/using_lib.md +++ b/src/doc/rust-by-example/src/crates/using_lib.md @@ -20,7 +20,7 @@ fn main() { ```txt # Where library.rlib is the path to the compiled library, assumed that it's # in the same directory here: -$ rustc executable.rs --extern rary=library.rlib --edition=2018 && ./executable +$ rustc executable.rs --extern rary=library.rlib && ./executable called rary's `public_function()` called rary's `indirect_access()`, that > called rary's `private_function()` diff --git a/src/doc/rust-by-example/src/fn/closures.md b/src/doc/rust-by-example/src/fn/closures.md index 0c1b999ef..82286003b 100644 --- a/src/doc/rust-by-example/src/fn/closures.md +++ b/src/doc/rust-by-example/src/fn/closures.md @@ -19,21 +19,23 @@ Other characteristics of closures include: ```rust,editable fn main() { - // Increment via closures and functions. - fn function(i: i32) -> i32 { i + 1 } + let outer_var = 42; + + // A regular function can't refer to variables in the enclosing environment + //fn function(i: i32) -> i32 { i + outer_var } + // TODO: uncomment the line above and see the compiler error. The compiler + // suggests that we define a closure instead. // Closures are anonymous, here we are binding them to references // Annotation is identical to function annotation but is optional // as are the `{}` wrapping the body. These nameless functions // are assigned to appropriately named variables. - let closure_annotated = |i: i32| -> i32 { i + 1 }; - let closure_inferred = |i | i + 1 ; - - let i = 1; - // Call the function and closures. - println!("function: {}", function(i)); - println!("closure_annotated: {}", closure_annotated(i)); - println!("closure_inferred: {}", closure_inferred(i)); + let closure_annotated = |i: i32| -> i32 { i + outer_var }; + let closure_inferred = |i | i + outer_var ; + + // Call the closures. + println!("closure_annotated: {}", closure_annotated(1)); + println!("closure_inferred: {}", closure_inferred(1)); // Once closure's type has been inferred, it cannot be inferred again with another type. //println!("cannot reuse closure_inferred with another type: {}", closure_inferred(42i64)); // TODO: uncomment the line above and see the compiler error. diff --git a/src/doc/rust-by-example/src/fn/closures/input_parameters.md b/src/doc/rust-by-example/src/fn/closures/input_parameters.md index 8f4307ff5..41497179f 100644 --- a/src/doc/rust-by-example/src/fn/closures/input_parameters.md +++ b/src/doc/rust-by-example/src/fn/closures/input_parameters.md @@ -22,7 +22,7 @@ closure. This is because if a move is possible, then any type of borrow should also be possible. Note that the reverse is not true. If the parameter is annotated as `Fn`, then capturing variables by `&mut T` or `T` are not -allowed. +allowed. However, `&T` is allowed. In the following example, try swapping the usage of `Fn`, `FnMut`, and `FnOnce` to see what happens: diff --git a/src/doc/rust-by-example/src/hello/print.md b/src/doc/rust-by-example/src/hello/print.md index c28dd9125..cce838fc2 100644 --- a/src/doc/rust-by-example/src/hello/print.md +++ b/src/doc/rust-by-example/src/hello/print.md @@ -68,7 +68,7 @@ fn main() { // For Rust 1.58 and above, you can directly capture the argument from a // surrounding variable. Just like the above, this will output - // " 1". 5 white spaces and a "1". + // " 1". 4 white spaces and a "1". let number: f64 = 1.0; let width: usize = 5; println!("{number:>width$}"); diff --git a/src/doc/rust-by-example/src/scope/lifetime.md b/src/doc/rust-by-example/src/scope/lifetime.md index 33ffcae71..01c4bf405 100644 --- a/src/doc/rust-by-example/src/scope/lifetime.md +++ b/src/doc/rust-by-example/src/scope/lifetime.md @@ -26,7 +26,7 @@ fn main() { let borrow1 = &i; // `borrow1` lifetime starts. ──┐│ // ││ println!("borrow1: {}", borrow1); // ││ - } // `borrow1 ends. ──────────────────────────────────┘│ + } // `borrow1` ends. ─────────────────────────────────┘│ // │ // │ { // │ diff --git a/src/doc/rust-by-example/src/std_misc/file/read_lines.md b/src/doc/rust-by-example/src/std_misc/file/read_lines.md index 6792b1706..641eb972a 100644 --- a/src/doc/rust-by-example/src/std_misc/file/read_lines.md +++ b/src/doc/rust-by-example/src/std_misc/file/read_lines.md @@ -1,5 +1,39 @@ # `read_lines` +## Beginner friendly method +This method is NOT efficient. It's here for beginners +who can't understand the efficient method yet. + +```rust,no_run +use std::fs::File; +use std::io::{ self, BufRead, BufReader }; + +fn read_lines(filename: String) -> io::Lines> { + // Open the file in read-only mode. + let file = File::open(filename).unwrap(); + // Read the file line by line, and return an iterator of the lines of the file. + return io::BufReader::new(file).lines(); +} + +fn main() { + // Stores the iterator of lines of the file in lines variable. + let lines = read_lines("./hosts".to_string()); + // Iterate over the lines of the file, and in this case print them. + for line in lines { + println!("{}", line.unwrap()); + } +} +``` + +Running this program simply prints the lines individually. +```shell +$ echo -e "127.0.0.1\n192.168.0.1\n" > hosts +$ rustc read_lines.rs && ./read_lines +127.0.0.1 +192.168.0.1 +``` + +## Efficient method The method `lines()` returns an iterator over the lines of a file. diff --git a/src/doc/rust-by-example/src/types/cast.md b/src/doc/rust-by-example/src/types/cast.md index deed34cf7..3078d82c1 100644 --- a/src/doc/rust-by-example/src/types/cast.md +++ b/src/doc/rust-by-example/src/types/cast.md @@ -22,7 +22,7 @@ fn main() { let integer = decimal as u8; let character = integer as char; - // Error! There are limitations in conversion rules. + // Error! There are limitations in conversion rules. // A float cannot be directly converted to a char. let character = decimal as char; // FIXME ^ Comment out this line @@ -52,7 +52,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: println!(" 128 as a i8 is : {}", 128 as i8); @@ -61,21 +61,21 @@ fn main() { println!("1000 as a u8 is : {}", 1000 as u8); // and the value of 232 in 8-bit two's complement representation is -24 println!(" 232 as a i8 is : {}", 232 as i8); - - // Since Rust 1.45, the `as` keyword performs a *saturating cast* - // when casting from float to int. If the floating point value exceeds - // the upper bound or is less than the lower bound, the returned value + + // Since Rust 1.45, the `as` keyword performs a *saturating cast* + // when casting from float to int. If the floating point value exceeds + // the upper bound or is less than the lower bound, the returned value // will be equal to the bound crossed. - + // 300.0 as u8 is 255 println!(" 300.0 as u8 is : {}", 300.0_f32 as u8); // -100.0 as u8 is 0 println!("-100.0 as u8 is : {}", -100.0_f32 as u8); // nan as u8 is 0 println!(" nan as u8 is : {}", f32::NAN as u8); - - // This behavior incurs a small runtime cost and can be avoided - // with unsafe methods, however the results might overflow and + + // This behavior incurs a small runtime cost and can be avoided + // with unsafe methods, however the results might overflow and // return **unsound values**. Use these methods wisely: unsafe { // 300.0 as u8 is 44 diff --git a/src/doc/rust-by-example/triagebot.toml b/src/doc/rust-by-example/triagebot.toml index fa0824ac5..5b70fd0c4 100644 --- a/src/doc/rust-by-example/triagebot.toml +++ b/src/doc/rust-by-example/triagebot.toml @@ -1 +1,4 @@ [assign] + +[assign.owners] +"*" = ["@marioidival"] -- cgit v1.2.3