const-cstr 0.2.1

Create static C-compatible strings from Rust string literals.

Crate const_cstr [] [src]

Create static C-compatible strings from Rust string literals.

Example

#[macro_use] extern crate const_cstr;
// Just for the `libc::c_char` type alias.
extern crate libc;
     
use std::ffi::CStr;

const_cstr! {
    HELLO_CSTR = "Hello, world!";

    // Multiple declarations can be made with one invocation.
    // GOODNIGHT_CSTR = "Goodnight, sun!";

    // But only with the same visibility:
    // pub GOODNIGHT_CSTR = "Goodnight, sun!";
    // ^~~ Error: expected identifier, found `pub` 
}

// Imagine this is an `extern "C"` function linked from some other lib.
unsafe fn print_c_string(cstr: *const libc::c_char) {
    println!("{}", CStr::from_ptr(cstr).to_str().unwrap());
}

fn main() {
    // When just passed a literal, returns an rvalue instead.
    let goodnight_cstr = const_cstr!("Goodnight, sun!");

    unsafe {
        print_c_string(HELLO_CSTR.as_ptr());
        print_c_string(goodnight_cstr.as_ptr());
    }
}

Prints:

Hello, world!
Goodnight, sun!

Macros

const_cstr!

Create a C-compatible string as an rvalue or a const binding. Appends a NUL byte to the passed string.

Structs

ConstCStr

A type representing a static C-compatible string, wrapping &'static str.