@@ -451,7 +451,7 @@ them.
451
451
~~~no_run
452
452
extern crate libc;
453
453
454
- use std::c_str::ToCStr ;
454
+ use std::ffi::CString ;
455
455
use std::ptr;
456
456
457
457
#[link(name = "readline")]
@@ -460,11 +460,10 @@ extern {
460
460
}
461
461
462
462
fn main() {
463
- "[my-awesome-shell] $".with_c_str(|buf| {
464
- unsafe { rl_prompt = buf; }
465
- // get a line, process it
466
- unsafe { rl_prompt = ptr::null(); }
467
- });
463
+ let prompt = CString::from_slice(b"[my-awesome-shell] $");
464
+ unsafe { rl_prompt = prompt.as_ptr(); }
465
+ // get a line, process it
466
+ unsafe { rl_prompt = ptr::null(); }
468
467
}
469
468
~~~
470
469
@@ -509,23 +508,28 @@ to define a block for all windows systems, not just x86 ones.
509
508
510
509
# Interoperability with foreign code
511
510
512
- Rust guarantees that the layout of a `struct` is compatible with the platform's representation in C
513
- only if the `#[repr(C)]` attribute is applied to it. `#[repr(C, packed)]` can be used to lay out
514
- struct members without padding. `#[repr(C)]` can also be applied to an enum.
515
-
516
- Rust's owned boxes (`Box<T>`) use non-nullable pointers as handles which point to the contained
517
- object. However, they should not be manually created because they are managed by internal
518
- allocators. References can safely be assumed to be non-nullable pointers directly to the type.
519
- However, breaking the borrow checking or mutability rules is not guaranteed to be safe, so prefer
520
- using raw pointers (`*`) if that's needed because the compiler can't make as many assumptions about
521
- them.
522
-
523
- Vectors and strings share the same basic memory layout, and utilities are available in the `vec` and
524
- `str` modules for working with C APIs. However, strings are not terminated with `\0`. If you need a
525
- NUL-terminated string for interoperability with C, you should use the `c_str::to_c_str` function.
526
-
527
- The standard library includes type aliases and function definitions for the C standard library in
528
- the `libc` module, and Rust links against `libc` and `libm` by default.
511
+ Rust guarantees that the layout of a `struct` is compatible with the platform's
512
+ representation in C only if the `#[repr(C)]` attribute is applied to it.
513
+ `#[repr(C, packed)]` can be used to lay out struct members without padding.
514
+ `#[repr(C)]` can also be applied to an enum.
515
+
516
+ Rust's owned boxes (`Box<T>`) use non-nullable pointers as handles which point
517
+ to the contained object. However, they should not be manually created because
518
+ they are managed by internal allocators. References can safely be assumed to be
519
+ non-nullable pointers directly to the type. However, breaking the borrow
520
+ checking or mutability rules is not guaranteed to be safe, so prefer using raw
521
+ pointers (`*`) if that's needed because the compiler can't make as many
522
+ assumptions about them.
523
+
524
+ Vectors and strings share the same basic memory layout, and utilities are
525
+ available in the `vec` and `str` modules for working with C APIs. However,
526
+ strings are not terminated with `\0`. If you need a NUL-terminated string for
527
+ interoperability with C, you should use the `CString` type in the `std::ffi`
528
+ module.
529
+
530
+ The standard library includes type aliases and function definitions for the C
531
+ standard library in the `libc` module, and Rust links against `libc` and `libm`
532
+ by default.
529
533
530
534
# The "nullable pointer optimization"
531
535
0 commit comments