Skip to content

Commit ece19bf

Browse files
committed
Enhance static mut example in FFI chapter.
Fixes #9980
1 parent cf636c2 commit ece19bf

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/doc/trpl/ffi.md

+11-4
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ fn main() {
420420
```
421421

422422
Alternatively, you may need to alter global state provided by a foreign
423-
interface. To do this, statics can be declared with `mut` so rust can mutate
423+
interface. To do this, statics can be declared with `mut` so we can mutate
424424
them.
425425

426426
```no_run
@@ -436,12 +436,19 @@ extern {
436436
437437
fn main() {
438438
let prompt = CString::from_slice(b"[my-awesome-shell] $");
439-
unsafe { rl_prompt = prompt.as_ptr(); }
440-
// get a line, process it
441-
unsafe { rl_prompt = ptr::null(); }
439+
unsafe {
440+
rl_prompt = prompt.as_ptr();
441+
442+
println!("{}", rl_prompt);
443+
444+
rl_prompt = ptr::null();
445+
}
442446
}
443447
```
444448

449+
Note that all interaction with a `static mut` is unsafe, both reading and
450+
writing. Dealing with global mutable state requires a great deal of care.
451+
445452
# Foreign calling conventions
446453

447454
Most foreign code exposes a C ABI, and Rust uses the platform's C calling convention by default when

0 commit comments

Comments
 (0)