diff options
| author | Steve Klabnik <steve@steveklabnik.com> | 2015-02-13 14:57:55 -0500 |
|---|---|---|
| committer | Steve Klabnik <steve@steveklabnik.com> | 2015-02-13 14:57:55 -0500 |
| commit | ece19bfc70b3742ebb8a2470accca7af96c5bc9a (patch) | |
| tree | 1c63307ecbadbca5587685727cd14764cbc50454 /src | |
| parent | cf636c233dfeef5abf0de8fb35e23c0a161810d2 (diff) | |
| download | rust-ece19bfc70b3742ebb8a2470accca7af96c5bc9a.tar.gz rust-ece19bfc70b3742ebb8a2470accca7af96c5bc9a.zip | |
Enhance static mut example in FFI chapter.
Fixes #9980
Diffstat (limited to 'src')
| -rw-r--r-- | src/doc/trpl/ffi.md | 15 |
1 files changed, 11 insertions, 4 deletions
diff --git a/src/doc/trpl/ffi.md b/src/doc/trpl/ffi.md index 640f9cc388e..5375a8aa46b 100644 --- a/src/doc/trpl/ffi.md +++ b/src/doc/trpl/ffi.md @@ -420,7 +420,7 @@ fn main() { ``` Alternatively, you may need to alter global state provided by a foreign -interface. To do this, statics can be declared with `mut` so rust can mutate +interface. To do this, statics can be declared with `mut` so we can mutate them. ```no_run @@ -436,12 +436,19 @@ extern { fn main() { let prompt = CString::from_slice(b"[my-awesome-shell] $"); - unsafe { rl_prompt = prompt.as_ptr(); } - // get a line, process it - unsafe { rl_prompt = ptr::null(); } + unsafe { + rl_prompt = prompt.as_ptr(); + + println!("{}", rl_prompt); + + rl_prompt = ptr::null(); + } } ``` +Note that all interaction with a `static mut` is unsafe, both reading and +writing. Dealing with global mutable state requires a great deal of care. + # Foreign calling conventions Most foreign code exposes a C ABI, and Rust uses the platform's C calling convention by default when |
