diff --git a/rustbook-en/listings/ch20-advanced-features/listing-20-11/src/main.rs b/rustbook-en/listings/ch20-advanced-features/listing-20-11/src/main.rs index 360e354..4e292b1 100644 --- a/rustbook-en/listings/ch20-advanced-features/listing-20-11/src/main.rs +++ b/rustbook-en/listings/ch20-advanced-features/listing-20-11/src/main.rs @@ -4,13 +4,15 @@ static mut COUNTER: u32 = 0; /// behavior, so you *must* guarantee you only call it from a single thread at /// a time. unsafe fn add_to_count(inc: u32) { - COUNTER += inc; + unsafe { + COUNTER += inc; + } } fn main() { unsafe { // SAFETY: This is only called from a single thread in `main`. add_to_count(3); - println!("COUNTER: {}", COUNTER); + println!("COUNTER: {}", *(&raw const COUNTER)); } } diff --git a/rustbook-en/packages/mdbook-trpl/src/listing/mod.rs b/rustbook-en/packages/mdbook-trpl/src/listing/mod.rs index 1087e9e..e1f7d00 100644 --- a/rustbook-en/packages/mdbook-trpl/src/listing/mod.rs +++ b/rustbook-en/packages/mdbook-trpl/src/listing/mod.rs @@ -252,7 +252,7 @@ impl Listing { fn opening_text(&self) -> String { self.file_name .as_ref() - .map(|file_name| format!("Filename: {file_name}\n")) + .map(|file_name| format!("{file_name}\n")) .unwrap_or_default() } diff --git a/rustbook-en/packages/mdbook-trpl/src/listing/tests.rs b/rustbook-en/packages/mdbook-trpl/src/listing/tests.rs index 4efb247..74790a8 100644 --- a/rustbook-en/packages/mdbook-trpl/src/listing/tests.rs +++ b/rustbook-en/packages/mdbook-trpl/src/listing/tests.rs @@ -51,7 +51,7 @@ Trailing text."#, &result.unwrap(), r#"Leading text. -Filename: src/main.rs +src/main.rs ```rust fn main() {} diff --git a/rustbook-en/src/appendix-01-keywords.md b/rustbook-en/src/appendix-01-keywords.md index 1df1691..8e00f34 100644 --- a/rustbook-en/src/appendix-01-keywords.md +++ b/rustbook-en/src/appendix-01-keywords.md @@ -69,6 +69,9 @@ Rust for potential future use. - `box` - `do` - `final` + +* `gen` + - `macro` - `override` - `priv` @@ -124,14 +127,14 @@ This code will compile without any errors. Note the `r#` prefix on the function name in its definition as well as where the function is called in `main`. Raw identifiers allow you to use any word you choose as an identifier, even if -that word happens to be a reserved keyword. This gives us more freedom to -choose identifier names, as well as lets us integrate with programs written in -a language where these words aren’t keywords. In addition, raw identifiers -allow you to use libraries written in a different Rust edition than your crate -uses. For example, `try` isn’t a keyword in the 2015 edition but is in the 2018 -edition. If you depend on a library that’s written using the 2015 edition and -has a `try` function, you’ll need to use the raw identifier syntax, `r#try` in -this case, to call that function from your 2018 edition code. See [Appendix -E][appendix-e] for more information on editions. +that word happens to be a reserved keyword. This gives us more freedom to choose +identifier names, as well as lets us integrate with programs written in a +language where these words aren’t keywords. In addition, raw identifiers allow +you to use libraries written in a different Rust edition than your crate uses. +For example, `try` isn’t a keyword in the 2015 edition but is in the 2018, 2021, +and 2024 editions. If you depend on a library that’s written using the 2015 +edition and has a `try` function, you’ll need to use the raw identifier syntax, +`r#try` in this case, to call that function from your 2018 edition code. See +[Appendix E][appendix-e] for more information on editions. [appendix-e]: appendix-05-editions.html diff --git a/rustbook-en/src/ch20-01-unsafe-rust.md b/rustbook-en/src/ch20-01-unsafe-rust.md index 703c957..724dd10 100644 --- a/rustbook-en/src/ch20-01-unsafe-rust.md +++ b/rustbook-en/src/ch20-01-unsafe-rust.md @@ -424,12 +424,13 @@ static variable named `COUNTER`. As with regular variables, we specify mutability using the `mut` keyword. Any -code that reads or writes from `COUNTER` must be within an `unsafe` block. This -code compiles and prints `COUNTER: 3` as we would expect because it’s single -threaded. Having multiple threads access `COUNTER` would likely result in data -races, so it is undefined behavior. Therefore, we need to mark the entire -function as `unsafe`, and document the safety limitation, so anyone calling the -function knows what they are and are not allowed to do safely. +code that reads or writes from `COUNTER` must be within an `unsafe` block. The +code in Listing 20-11 compiles and prints `COUNTER: 3` as we would expect +because it’s single threaded. Having multiple threads access `COUNTER` would +likely result in data races, so it is undefined behavior. Therefore, we need to +mark the entire function as `unsafe`, and document the safety limitation, so +anyone calling the function knows what they are and are not allowed to do +safely. Whenever we write an unsafe function, it is idiomatic to write a comment starting with `SAFETY` and explaining what the caller needs to do to call the @@ -437,6 +438,13 @@ function safely. Likewise, whenever we perform an unsafe operation, it is idiomatic to write a comment starting with `SAFETY` to explain how the safety rules are upheld. +Additionally, the compiler will not allow you to create references to a mutable +static variable. You can only access it via a raw pointer, created with one of +the raw borrow operators. That includes in cases where the reference is created +invisibly, as when it is used in the `println!` in this code listing. The +requirement that references to static mutable variables can only be created via +raw pointers helps make the safety requirements for using them more obvious. + With mutable data that is globally accessible, it’s difficult to ensure there are no data races, which is why Rust considers mutable static variables to be unsafe. Where possible, it’s preferable to use the concurrency techniques and