RS.CLIPPY.MANUAL_C_STR_LITERALS

R#"creating a `CStr` through functions when `c""` literals can be used"#

This checker is a Clippy lint created by The Rust Project Contributors. The documentation shown here is a copy of the original documentation for: manual_c_str_literals. Copyright ©2025 The Rust Team. All rights reserved.

What it does

Checks for the manual creation of C strings (a string with a NUL byte at the end), either through one of the CStr constructor functions, or more plainly by calling .as_ptr() on a (byte) string literal with a hardcoded \\0 byte at the end.

Why is this bad?

This can be written more concisely using c"str" literals and is also less error-prone, because the compiler checks for interior NUL bytes and the terminating NUL byte is inserted automatically.

Example

fn needs_cstr(_: &CStr) {}

needs_cstr(CStr::from_bytes_with_nul(b"Hello\\0").unwrap());
unsafe { libc::puts("World\\0".as_ptr().cast()) }

Use instead:

fn needs_cstr(_: &CStr) {}

needs_cstr(c"Hello");
unsafe { libc::puts(c"World".as_ptr()) }

Configuration

  • msrv: The minimum rust version that the project supports. Defaults to the rust-version field in Cargo.toml

    (default: current version)