Skip to main content

Module string

Module string 

Source
Expand description

Utilities for safely moving UTF-8 strings across the FFI boundary.

Interoperability between Rust and C/C++/Python often requires raw pointers to null terminated strings. This module provides conversion functions that:

  • Convert raw *const c_char pointers to Rust String, &str, byte slices, or ustr::Ustr values.
  • Perform the inverse conversion when Rust needs to hand ownership of a string to foreign code.

The majority of these functions are marked unsafe because they accept raw pointers and rely on the caller to uphold basic invariants (pointer validity, lifetime, UTF-8 correctness). Each function documents the specific safety requirements.

Functions§

cstr_as_str⚠
Convert a C string pointer into a borrowed string slice.
cstr_drop⚠
Drops the C string memory at the pointer.
cstr_to_bytes⚠
Convert a C string pointer into a borrowed byte slice.
cstr_to_ustr⚠
Convert a C string pointer into an owned Ustr.
optional_cstr_to_str⚠
Convert an optional C string pointer into Option<&str>.
optional_cstr_to_ustr⚠
Convert a C string pointer into an owned Option<Ustr>.
pystr_to_string⚠
Returns an owned string from a valid Python object pointer.
str_to_cstr
Create a C string pointer to newly allocated memory from a &str.