alloc::ffi - Rust (original) (raw)

Module ffi

1.64.0 · Source

Expand description

Utilities related to FFI bindings.

This module provides utilities to handle data across non-Rust interfaces, like other programming languages and the underlying operating system. It is mainly of use for FFI (Foreign Function Interface) bindings and code that needs to exchange C-like strings with other languages.

§Overview

Rust represents owned strings with the String type, and borrowed slices of strings with the str primitive. Both are always in UTF-8 encoding, and may contain nul bytes in the middle, i.e., if you look at the bytes that make up the string, there may be a \0 among them. Both String and str store their length explicitly; there are no nul terminators at the end of strings like in C.

C strings are different from Rust strings:

§Representations of non-Rust strings

CString and CStr are useful when you need to transfer UTF-8 strings to and from languages with a C ABI, like Python.

c_str

CString and its related types.

CString

A type representing an owned, C-compatible, nul-terminated string with no nul bytes in the middle.

FromVecWithNulError

An error indicating that a nul byte was not in the expected position.

IntoStringError

An error indicating invalid UTF-8 when converting a CString into a String.

NulError

An error indicating that an interior nul byte was found.