Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/rust-lang-uz/book
Browse files Browse the repository at this point in the history
  • Loading branch information
0xf90c committed Jan 19, 2025
2 parents 2a45bea + 017bc44 commit 9edc73c
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
2 changes: 1 addition & 1 deletion rustbook-en/packages/mdbook-trpl/src/listing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
2 changes: 1 addition & 1 deletion rustbook-en/packages/mdbook-trpl/src/listing/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Trailing text."#,
&result.unwrap(),
r#"Leading text.
Filename: src/main.rs
src/main.rs
```rust
fn main() {}
Expand Down
21 changes: 12 additions & 9 deletions rustbook-en/src/appendix-01-keywords.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ Rust for potential future use.
- `box`
- `do`
- `final`

* `gen`

- `macro`
- `override`
- `priv`
Expand Down Expand Up @@ -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]<!-- ignore --> 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]<!-- ignore --> for more information on editions.

[appendix-e]: appendix-05-editions.html
20 changes: 14 additions & 6 deletions rustbook-en/src/ch20-01-unsafe-rust.md
Original file line number Diff line number Diff line change
Expand Up @@ -424,19 +424,27 @@ static variable named `COUNTER`.
</Listing>
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
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
Expand Down

0 comments on commit 9edc73c

Please sign in to comment.