liveBook · Manning

C_char

As explained in Refactoring to Rust

c_char is a libc Rust type corresponding to the platform-specific C char primitive for foreign-function interfaces.

c_char is the libc type used for a platform-specific C char in Rust foreign-function interfaces. Import it from libc when matching a C function signature. section 3.2

A C function accepting char * becomes a Rust function accepting *const c_char when it only reads the character data. The pointer type records that distinction in the signature. section 3.2

Matching a C function signature

use libc::{c_char, c_int}; fn solve(line: *const c_char, solution: *mut c_int) -> c_int { 0 } Here, c_char represents the C character type, while the raw pointer expresses the incoming character buffer used by the foreign-function boundary. section 3.2.1 section 3.2

Listing 3.4 The most basic solve function in Rust that compiles
use libc::{c_char, c_int};  #1

fn solve(line: *const c_char, solution: *mut c_int) -> c_int {
  0  #2
}

Inspect the smallest compiling Rust function that imports c_char and uses it in a C-compatible pointer parameter. section 3.2.1

How Refactoring to Rust covers it

How deep it goes
Defines platform sizing and demonstrates a compiling signature, but defers character-buffer handling and broader FFI implementation.
Examples
Uses a solve function receiving *const c_char and returning c_int alongside a mutable c_int output pointer.
What it assumes
Assumes basic C function signatures and pointers while introducing libc imports and Rust raw-pointer mutability.
What you can do after
Apply it via the libc crate to write Rust signatures compatible with C character-pointer parameters.
How it connects
Connects c_char with libc, C FFI, raw pointers, and c_int.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Refactoring to Rust. The section links point to the parts of the book it draws on.