Skip to content
Closed
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 listings/ch20-advanced-features/listing-20-08/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
unsafe extern "C" {
extern "C" {
fn abs(input: i32) -> i32;
}

Expand Down
2 changes: 1 addition & 1 deletion listings/ch20-advanced-features/listing-20-09/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
unsafe extern "C" {
extern "C" {
safe fn abs(input: i32) -> i32;
}

Expand Down
8 changes: 4 additions & 4 deletions src/ch20-01-unsafe-rust.md
Original file line number Diff line number Diff line change
Expand Up @@ -324,22 +324,22 @@ programmer to ensure safety.

</Listing>

Within the `unsafe extern "C"` block, we list the names and signatures of
Within the `extern "C"` block, we list the names and signatures of
external functions from another language we want to call. The `"C"` part
defines which _application binary interface (ABI)_ the external function uses:
the ABI defines how to call the function at the assembly level. The `"C"` ABI
is the most common and follows the C programming language’s ABI. Information
about all the ABIs Rust supports is available in [the Rust Reference][ABI].

Every item declared within an `unsafe extern` block is implicitly unsafe.
Every item declared within an `extern` block is implicitly unsafe.
However, some FFI functions *are* safe to call. For example, the `abs` function
from C’s standard library does not have any memory safety considerations and we
know it can be called with any `i32`. In cases like this, we can use the `safe`
keyword to say that this specific function is safe to call even though it is in
an `unsafe extern` block. Once we make that change, calling it no longer
an `extern` block. Once we make that change, calling it no longer
requires an `unsafe` block, as shown in Listing 20-9.

<Listing number="20-9" file-name="src/main.rs" caption="Explicitly marking a function as `safe` within an `unsafe extern` block and calling it safely">
<Listing number="20-9" file-name="src/main.rs" caption="Explicitly marking a function as `safe` within an `extern` block and calling it safely">

```rust
{{#rustdoc_include ../listings/ch20-advanced-features/listing-20-09/src/main.rs}}
Expand Down
Loading