Skip to content

Commit 28d1fb5

Browse files
committed
Say "use" rather than "call" for unsafe extern items
We can have static items as well as function items within an `extern` block, so let's say "use" rather than "call" when referring to these. Let's also give an example of a static item to show how `safe` allows for safe access to these items.
1 parent de10a88 commit 28d1fb5

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

posts/2024-10-17-Rust-1.82.0.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,20 +170,21 @@ The native syntax makes it more clear that the operand expression of these opera
170170

171171
### Safe items with `unsafe extern`
172172

173-
Rust code can use functions and statics from foreign code. The type signatures of these foreign items are provided in `extern` blocks. Historically, all items within `extern` blocks have been unsafe to call, but we didn't have to write `unsafe` anywhere on the `extern` block itself.
173+
Rust code can use functions and statics from foreign code. The type signatures of these foreign items are provided in `extern` blocks. Historically, all items within `extern` blocks have been unsafe to use, but we didn't have to write `unsafe` anywhere on the `extern` block itself.
174174

175175
However, if a signature within the `extern` block is incorrect, then using that item will result in undefined behavior. Would that be the fault of the person who wrote the `extern` block, or the person who used that item?
176176

177177
We've decided that it's the responsibility of the person writing the `extern` block to ensure that all signatures contained within it are correct, and so we now allow writing `unsafe extern`:
178178

179179
```rust
180180
unsafe extern {
181+
pub safe static TAU: f64;
181182
pub safe fn sqrt(x: f64) -> f64;
182183
pub unsafe fn strlen(p: *const u8) -> usize;
183184
}
184185
```
185186

186-
One benefit of this is that items within an `unsafe extern` block can be marked as safe to call. In the above example, we can call `sqrt` without using `unsafe`. Items that aren't marked with either `safe` or `unsafe` are conservatively assumed to be `unsafe`.
187+
One benefit of this is that items within an `unsafe extern` block can be marked as safe to use. In the above example, we can call `sqrt` or read `TAU` without using `unsafe`. Items that aren't marked with either `safe` or `unsafe` are conservatively assumed to be `unsafe`.
187188

188189
In future releases, we'll be encouraging the use of `unsafe extern` with lints. Starting in Rust 2024, using `unsafe extern` will be required.
189190

0 commit comments

Comments
 (0)