Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to 2024 edition #481

Merged
merged 1 commit into from
Mar 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
merge_group:

env:
MDBOOK_VERSION: 0.4.40
MDBOOK_VERSION: 0.4.45

jobs:
test:
Expand Down
2 changes: 1 addition & 1 deletion book.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ git-repository-url = "https://github.com/rust-lang/nomicon"
"./arc.html" = "./arc-mutex/arc.html"

[rust]
edition = "2021"
edition = "2024"
2 changes: 1 addition & 1 deletion src/beneath-std.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ use core::ffi::{c_char, c_int};
use core::panic::PanicInfo;

// Entry point for this program.
#[no_mangle] // ensure that this symbol is included in the output as `main`
#[unsafe(no_mangle)] // ensure that this symbol is included in the output as `main`
extern "C" fn main(_argc: c_int, _argv: *const *const c_char) -> c_int {
0
}
Expand Down
42 changes: 21 additions & 21 deletions src/ffi.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ compile if snappy is installed:
use libc::size_t;

#[link(name = "snappy")]
extern {
unsafe extern "C" {
fn snappy_max_compressed_length(source_length: size_t) -> size_t;
}

Expand Down Expand Up @@ -64,7 +64,7 @@ The `extern` block can be extended to cover the entire snappy API:
use libc::{c_int, size_t};

#[link(name = "snappy")]
extern {
unsafe extern {
fn snappy_compress(input: *const u8,
input_length: size_t,
compressed: *mut u8,
Expand Down Expand Up @@ -251,7 +251,7 @@ First, we assume you have a lib crate named as `rust_from_c`.
`lib.rs` should have Rust code as following:

```rust
#[no_mangle]
#[unsafe(no_mangle)]
pub extern "C" fn hello_from_rust() {
println!("Hello from Rust!");
}
Expand Down Expand Up @@ -331,7 +331,7 @@ extern fn callback(a: i32) {
}

#[link(name = "extlib")]
extern {
unsafe extern {
fn register_callback(cb: extern fn(i32)) -> i32;
fn trigger_callback();
}
Expand Down Expand Up @@ -383,7 +383,7 @@ struct RustObject {
// Other members...
}

extern "C" fn callback(target: *mut RustObject, a: i32) {
unsafe extern "C" fn callback(target: *mut RustObject, a: i32) {
println!("I'm called from C with value {0}", a);
unsafe {
// Update the value in RustObject with the value received from the callback:
Expand All @@ -392,9 +392,9 @@ extern "C" fn callback(target: *mut RustObject, a: i32) {
}

#[link(name = "extlib")]
extern {
unsafe extern {
fn register_callback(target: *mut RustObject,
cb: extern fn(*mut RustObject, i32)) -> i32;
cb: unsafe extern fn(*mut RustObject, i32)) -> i32;
fn trigger_callback();
}

Expand Down Expand Up @@ -523,7 +523,7 @@ blocks with the `static` keyword:
<!-- ignore: requires libc crate -->
```rust,ignore
#[link(name = "readline")]
extern {
unsafe extern {
static rl_readline_version: libc::c_int;
}

Expand All @@ -543,7 +543,7 @@ use std::ffi::CString;
use std::ptr;

#[link(name = "readline")]
extern {
unsafe extern {
static mut rl_prompt: *const libc::c_char;
}

Expand Down Expand Up @@ -573,7 +573,7 @@ conventions. Rust provides a way to tell the compiler which convention to use:
#[cfg(all(target_os = "win32", target_arch = "x86"))]
#[link(name = "kernel32")]
#[allow(non_snake_case)]
extern "stdcall" {
unsafe extern "stdcall" {
fn SetEnvironmentVariableA(n: *const u8, v: *const u8) -> libc::c_int;
}
# fn main() { }
Expand Down Expand Up @@ -635,7 +635,7 @@ In C, functions can be 'variadic', meaning they accept a variable number of argu
be achieved in Rust by specifying `...` within the argument list of a foreign function declaration:

```no_run
extern {
unsafe extern {
fn foo(x: i32, ...);
}

Expand Down Expand Up @@ -685,7 +685,7 @@ we have function pointers flying across the FFI boundary in both directions.
use libc::c_int;

# #[cfg(hidden)]
extern "C" {
unsafe extern "C" {
/// Registers the callback.
fn register(cb: Option<extern "C" fn(Option<extern "C" fn(c_int) -> c_int>, c_int) -> c_int>);
}
Expand Down Expand Up @@ -750,8 +750,8 @@ mechanisms (notably C++'s `try`/`catch`).

<!-- ignore: using unstable feature -->
```rust,ignore
#[no_mangle]
extern "C-unwind" fn example() {
#[unsafe(no_mangle)]
unsafe extern "C-unwind" fn example() {
panic!("Uh oh");
}
```
Expand Down Expand Up @@ -780,13 +780,13 @@ If the C++ frames have objects, their destructors will be called.
<!-- ignore: using unstable feature -->
```rust,ignore
#[link(...)]
extern "C-unwind" {
unsafe extern "C-unwind" {
// A C++ function that may throw an exception
fn may_throw();
}

#[no_mangle]
extern "C-unwind" fn rust_passthrough() {
#[unsafe(no_mangle)]
unsafe extern "C-unwind" fn rust_passthrough() {
let b = Box::new(5);
unsafe { may_throw(); }
println!("{:?}", &b);
Expand Down Expand Up @@ -816,7 +816,7 @@ will be printed.
### `panic` can be stopped at an ABI boundary

```rust
#[no_mangle]
#[unsafe(no_mangle)]
extern "C" fn assert_nonzero(input: u32) {
assert!(input != 0)
}
Expand All @@ -833,7 +833,7 @@ process if it panics, you must use [`catch_unwind`]:
```rust
use std::panic::catch_unwind;

#[no_mangle]
#[unsafe(no_mangle)]
pub extern "C" fn oh_no() -> i32 {
let result = catch_unwind(|| {
panic!("Oops!");
Expand Down Expand Up @@ -867,7 +867,7 @@ We can represent this in Rust with the `c_void` type:

<!-- ignore: requires libc crate -->
```rust,ignore
extern "C" {
unsafe extern "C" {
pub fn foo(arg: *mut libc::c_void);
pub fn bar(arg: *mut libc::c_void);
}
Expand Down Expand Up @@ -902,7 +902,7 @@ pub struct Bar {
core::marker::PhantomData<(*mut u8, core::marker::PhantomPinned)>,
}

extern "C" {
unsafe extern "C" {
pub fn foo(arg: *mut Foo);
pub fn bar(arg: *mut Bar);
}
Expand Down
2 changes: 1 addition & 1 deletion src/intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Topics that are within the scope of this book include: the meaning of (un)safety

The Rustonomicon is not a place to exhaustively describe the semantics and guarantees of every single API in the standard library, nor is it a place to exhaustively describe every feature of Rust.

Unless otherwise noted, Rust code in this book uses the Rust 2021 edition.
Unless otherwise noted, Rust code in this book uses the Rust 2024 edition.

[trpl]: ../book/index.html
[ref]: ../reference/index.html
2 changes: 1 addition & 1 deletion src/other-reprs.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ and it will become a hard error.
`repr(packed)/repr(packed(n))` is not to be used lightly. Unless you have
extreme requirements, this should not be used.

This repr is a modifier on `repr(C)` and `repr(Rust)`. For FFI compatibilty
This repr is a modifier on `repr(C)` and `repr(Rust)`. For FFI compatibility
you most likely always want to be explicit: `repr(C, packed)`.

## repr(align(n))
Expand Down
4 changes: 2 additions & 2 deletions src/send-and-sync.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ to the heap.
# pub use ::std::os::raw::{c_int, c_void};
# #[allow(non_camel_case_types)]
# pub type size_t = usize;
# extern "C" { pub fn posix_memalign(memptr: *mut *mut c_void, align: size_t, size: size_t) -> c_int; }
# unsafe extern "C" { pub fn posix_memalign(memptr: *mut *mut c_void, align: size_t, size: size_t) -> c_int; }
# }
use std::{
mem::{align_of, size_of},
Expand Down Expand Up @@ -225,7 +225,7 @@ allocation done on another thread. We can check this is true in the docs for
# struct Carton<T>(std::ptr::NonNull<T>);
# mod libc {
# pub use ::std::os::raw::c_void;
# extern "C" { pub fn free(p: *mut c_void); }
# unsafe extern "C" { pub fn free(p: *mut c_void); }
# }
impl<T> Drop for Carton<T> {
fn drop(&mut self) {
Expand Down