Struct std::ffi::CStringUnstable [-]  [+] [src]

pub struct CString {
    // some fields omitted
}

A type representing a C-compatible string

This type serves the primary purpose of being able to generate a C-compatible string from a Rust byte slice or vector. An instance of this type is a static guarantee that the underlying bytes contain no interior 0 bytes and the final byte is 0.

A CString is created from either a byte slice or a byte vector. After being created, a CString predominately inherits all of its methods from the Deref implementation to [libc::c_char]. Note that the underlying array is represented as an array of libc::c_char as opposed to u8. A u8 slice can be obtained with the as_bytes method. Slices produced from a CString do not contain the trailing nul terminator unless otherwise specified.

Example

extern crate libc; fn main() { use std::ffi::CString; use libc; extern { fn my_printer(s: *const libc::c_char); } let to_print = "Hello, world!"; let c_to_print = CString::from_slice(to_print.as_bytes()); unsafe { my_printer(c_to_print.as_ptr()); } }
use std::ffi::CString;
use libc;

extern {
    fn my_printer(s: *const libc::c_char);
}

let to_print = "Hello, world!";
let c_to_print = CString::from_slice(to_print.as_bytes());
unsafe {
    my_printer(c_to_print.as_ptr());
}

Methods

impl CString

fn from_slice(v: &[u8]) -> CString

Create a new C-compatible string from a byte slice.

This method will copy the data of the slice provided into a new allocation, ensuring that there is a trailing 0 byte.

Panics

This function will panic if there are any 0 bytes already in the slice provided.

fn from_vec(v: Vec<u8>) -> CString

Create a C-compatible string from a byte vector.

This method will consume ownership of the provided vector, appending a 0 byte to the end after verifying that there are no interior 0 bytes.

Panics

This function will panic if there are any 0 bytes already in the vector provided.

unsafe fn from_vec_unchecked(v: Vec<u8>) -> CString

Create a C-compatible string from a byte vector without checking for interior 0 bytes.

This method is equivalent to from_vec except that no runtime assertion is made that v contains no 0 bytes.

fn as_slice_with_nul(&self) -> &[c_char]

Create a view into this C string which includes the trailing nul terminator at the end of the string.

fn as_bytes(&self) -> &[u8]

Similar to the as_slice method, but returns a u8 slice instead of a libc::c_char slice.

fn as_bytes_with_nul(&self) -> &[u8]

Equivalent to as_slice_with_nul except that the type returned is a u8 slice instead of a libc::c_char slice.

Trait Implementations

impl Deref for CString

type Target = [c_char]

fn deref(&self) -> &[c_char]

impl Show for CString

fn fmt(&self, f: &mut Formatter) -> Result

impl BytesContainer for CString

fn container_as_bytes<'a>(&'a self) -> &'a [u8]

fn container_as_str<'a>(&'a self) -> Option<&'a str>

fn is_str(_: Option<&Self>) -> bool

Derived Implementations

impl<__S: Writer + Hasher> Hash<__S> for CString

fn hash(&self, __arg_0: &mut __S)

impl Ord for CString

fn cmp(&self, __arg_0: &CString) -> Ordering

impl Eq for CString

impl PartialOrd for CString

fn partial_cmp(&self, __arg_0: &CString) -> Option<Ordering>

fn lt(&self, __arg_0: &CString) -> bool

fn le(&self, __arg_0: &CString) -> bool

fn gt(&self, __arg_0: &CString) -> bool

fn ge(&self, __arg_0: &CString) -> bool

impl PartialEq for CString

fn eq(&self, __arg_0: &CString) -> bool

fn ne(&self, __arg_0: &CString) -> bool

impl Clone for CString

fn clone(&self) -> CString

fn clone_from(&mut self, source: &Self)