From 8515f067deef63a6c637497586a5bc66e6bc9e13 Mon Sep 17 00:00:00 2001 From: Ravi Makhija Date: Tue, 24 Oct 2023 07:50:09 -0400 Subject: [PATCH 1/5] Correct exclusive bound in guard example --- src/flow_control/match/guard.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/flow_control/match/guard.md b/src/flow_control/match/guard.md index af81f64c99..df9fb2c9ae 100644 --- a/src/flow_control/match/guard.md +++ b/src/flow_control/match/guard.md @@ -16,10 +16,10 @@ fn main() { match temperature { Temperature::Celsius(t) if t > 30 => println!("{}C is above 30 Celsius", t), // The `if condition` part ^ is a guard - Temperature::Celsius(t) => println!("{}C is below 30 Celsius", t), + Temperature::Celsius(t) => println!("{}C is equal to or below 30 Celsius", t), Temperature::Fahrenheit(t) if t > 86 => println!("{}F is above 86 Fahrenheit", t), - Temperature::Fahrenheit(t) => println!("{}F is below 86 Fahrenheit", t), + Temperature::Fahrenheit(t) => println!("{}F is equal to or below 86 Fahrenheit", t), } } ``` From c6e0d7b9bf96f5f51bf736e4eb92f8a867db769b Mon Sep 17 00:00:00 2001 From: Ravi Makhija Date: Sun, 29 Oct 2023 12:55:06 -0400 Subject: [PATCH 2/5] Removed unused statement from `or_else` example --- src/error/option_unwrap/defaults.md | 1 - 1 file changed, 1 deletion(-) diff --git a/src/error/option_unwrap/defaults.md b/src/error/option_unwrap/defaults.md index 513bb1df71..e439f5cd41 100644 --- a/src/error/option_unwrap/defaults.md +++ b/src/error/option_unwrap/defaults.md @@ -38,7 +38,6 @@ Another alternative is to use `or_else`, which is also chainable, and evaluates enum Fruit { Apple, Orange, Banana, Kiwi, Lemon } fn main() { - let apple = Some(Fruit::Apple); let no_fruit: Option = None; let get_kiwi_as_fallback = || { println!("Providing kiwi as fallback"); From b20ef1611a1e8b41aa8b1103fb8b1a515d75d693 Mon Sep 17 00:00:00 2001 From: Ravi Makhija Date: Fri, 3 Nov 2023 07:28:33 -0400 Subject: [PATCH 3/5] Fix various minor typos --- src/error/multiple_error_types/boxing_errors.md | 2 +- src/fn/closures/capture.md | 2 +- src/meta/playground.md | 2 +- src/trait/clone.md | 4 ++-- src/unsafe/asm.md | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/error/multiple_error_types/boxing_errors.md b/src/error/multiple_error_types/boxing_errors.md index 84b0c41e55..ca506d4131 100644 --- a/src/error/multiple_error_types/boxing_errors.md +++ b/src/error/multiple_error_types/boxing_errors.md @@ -12,7 +12,7 @@ via [`From`][from]. use std::error; use std::fmt; -// Change the alias to `Box`. +// Change the alias to use `Box`. type Result = std::result::Result>; #[derive(Debug, Clone)] diff --git a/src/fn/closures/capture.md b/src/fn/closures/capture.md index 061ef1c7b2..c2f0c62acb 100644 --- a/src/fn/closures/capture.md +++ b/src/fn/closures/capture.md @@ -44,7 +44,7 @@ fn main() { // borrows `count`. // // A `mut` is required on `inc` because a `&mut` is stored inside. Thus, - // calling the closure mutates the closure which requires a `mut`. + // calling the closure mutates `count` which requires a `mut`. let mut inc = || { count += 1; println!("`count`: {}", count); diff --git a/src/meta/playground.md b/src/meta/playground.md index e78552d298..6da4fdfdb2 100644 --- a/src/meta/playground.md +++ b/src/meta/playground.md @@ -14,7 +14,7 @@ fn main() { ``` This allows the reader to both run your code sample, but also modify and tweak -it. The key here is the adding the word `editable` to your codefence block +it. The key here is the adding of the word `editable` to your codefence block separated by a comma. ````markdown diff --git a/src/trait/clone.md b/src/trait/clone.md index 5d6747a47b..8e04f5a2ae 100644 --- a/src/trait/clone.md +++ b/src/trait/clone.md @@ -40,11 +40,11 @@ fn main() { // Clone `moved_pair` into `cloned_pair` (resources are included) let cloned_pair = moved_pair.clone(); - // Drop the original pair using std::mem::drop + // Drop the moved original pair using std::mem::drop drop(moved_pair); // Error! `moved_pair` has been dropped - //println!("copy: {:?}", moved_pair); + //println!("moved and dropped: {:?}", moved_pair); // TODO ^ Try uncommenting this line // The result from .clone() can still be used! diff --git a/src/unsafe/asm.md b/src/unsafe/asm.md index cbe52c8407..a4faee1ca7 100644 --- a/src/unsafe/asm.md +++ b/src/unsafe/asm.md @@ -165,7 +165,7 @@ assert_eq!(a, 12); ``` The above could work well in unoptimized cases (`Debug` mode), but if you want optimized performance (`release` mode or other optimized cases), it could not work. -That is because in optimized cases, the compiler is free to allocate the same register for inputs `b` and `c` since it knows they have the same value. However it must allocate a separate register for `a` since it uses `inout` and not `inlateout`. If `inlateout` was used, then `a` and `c` could be allocated to the same register, in which case the first instruction to overwrite the value of `c` and cause the assembly code to produce the wrong result. +That is because in optimized cases, the compiler is free to allocate the same register for inputs `b` and `c` since it knows they have the same value. However it must allocate a separate register for `a` since it uses `inout` and not `inlateout`. If `inlateout` was used, then `a` and `c` could be allocated to the same register, in which case the first instruction could overwrite the value of `c` and cause the assembly code to produce the wrong result. However the following example can use `inlateout` since the output is only modified after all input registers have been read: From ff939dd4e1866abd0b6e48f75cb079696f5fc4bb Mon Sep 17 00:00:00 2001 From: Ravi Makhija Date: Fri, 3 Nov 2023 07:33:24 -0400 Subject: [PATCH 4/5] Various minor edits for clarity --- src/error/multiple_error_types/reenter_question_mark.md | 2 +- src/macros/dsl.md | 2 +- src/trait/disambiguating.md | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/error/multiple_error_types/reenter_question_mark.md b/src/error/multiple_error_types/reenter_question_mark.md index 998b741e79..e00146462b 100644 --- a/src/error/multiple_error_types/reenter_question_mark.md +++ b/src/error/multiple_error_types/reenter_question_mark.md @@ -26,7 +26,7 @@ Here, we rewrite the previous example using `?`. As a result, the use std::error; use std::fmt; -// Change the alias to `Box`. +// Change the alias to use `Box`. type Result = std::result::Result>; #[derive(Debug)] diff --git a/src/macros/dsl.md b/src/macros/dsl.md index 9aaeda34cd..d83885a4d6 100644 --- a/src/macros/dsl.md +++ b/src/macros/dsl.md @@ -12,7 +12,7 @@ an expression and have the output printed to console. macro_rules! calculate { (eval $e:expr) => { { - let val: usize = $e; // Force types to be integers + let val: usize = $e; // Force types to be unsigned integers println!("{} = {}", stringify!{$e}, val); } }; diff --git a/src/trait/disambiguating.md b/src/trait/disambiguating.md index ae80d4acb8..6fca4b032f 100644 --- a/src/trait/disambiguating.md +++ b/src/trait/disambiguating.md @@ -1,8 +1,8 @@ # Disambiguating overlapping traits A type can implement many different traits. What if two traits both require -the same name? For example, many traits might have a method named `get()`. -They might even have different return types! +the same name for a function? For example, many traits might have a method +named `get()`. They might even have different return types! Good news: because each trait implementation gets its own `impl` block, it's clear which trait's `get` method you're implementing. From 9153f289ddffa82d1c582e5dbb605e3bd645c89b Mon Sep 17 00:00:00 2001 From: Ravi Makhija Date: Fri, 3 Nov 2023 07:35:36 -0400 Subject: [PATCH 5/5] Various minor formatting edits --- src/error/abort_unwind.md | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/src/error/abort_unwind.md b/src/error/abort_unwind.md index d38af3ce11..2afae98824 100644 --- a/src/error/abort_unwind.md +++ b/src/error/abort_unwind.md @@ -6,14 +6,17 @@ The previous section illustrates the error handling mechanism `panic`. Differen Building on the prior lemonade example, we explicitly use the panic strategy to exercise different lines of code. ```rust,editable,mdbook-runnable - fn drink(beverage: &str) { - // You shouldn't drink too much sugary beverages. + // You shouldn't drink too much sugary beverages. if beverage == "lemonade" { - if cfg!(panic="abort"){ println!("This is not your party. Run!!!!");} - else{ println!("Spit it out!!!!");} + if cfg!(panic = "abort") { + println!("This is not your party. Run!!!!"); + } else { + println!("Spit it out!!!!"); + } + } else { + println!("Some refreshing {} is all I need.", beverage); } - else{ println!("Some refreshing {} is all I need.", beverage); } } fn main() { @@ -25,16 +28,22 @@ fn main() { Here is another example focusing on rewriting `drink()` and explicitly use the `unwind` keyword. ```rust,editable - #[cfg(panic = "unwind")] -fn ah(){ println!("Spit it out!!!!");} +fn ah() { + println!("Spit it out!!!!"); +} -#[cfg(not(panic="unwind"))] -fn ah(){ println!("This is not your party. Run!!!!");} +#[cfg(not(panic = "unwind"))] +fn ah() { + println!("This is not your party. Run!!!!"); +} -fn drink(beverage: &str){ - if beverage == "lemonade"{ ah();} - else{println!("Some refreshing {} is all I need.", beverage);} +fn drink(beverage: &str) { + if beverage == "lemonade" { + ah(); + } else { + println!("Some refreshing {} is all I need.", beverage); + } } fn main() {