#format-string #part #native #printf #type #interpolate #traits

strprintf

Provide a way to interpolate printf-style format strings using native Rust types. Part of libnewsboat lib dependencies

1 unstable release

new 0.1.0 Dec 23, 2024

#795 in Rust patterns

Download history 297/week @ 2024-12-21

297 downloads per month
Used in 3 crates (2 directly)

MIT license

4MB
491 lines

Contains (ELF exe/lib, 4MB) build_script_build-5dca53877b6ab9f9, (ELF exe/lib, 4MB) build-script-build

Strprintf

This project is part of Newsboat Rust libraries, I'm not it's author - merely maintaining up to date versions on Crates.io.

Description

Problem statement for strprintf crate: provide a way to interpolate printf-style format strings using native Rust types. For example, it should be possible to format a string "%i %.2f %x" using values 42u32, 3.1415f64, and 255u8, and get a std::string::String "42 3.14 ff".

This is the same as strprintf module we already have in C .

The problem can be solved by wrapping libc::snprintf, which is what we do both here and in C . However, our experience with C showed that we should constrain what types can be formatted. Otherwise, complex objects like String will be passed over FFI and lead to unexpected results (e.g. garbage strings).

To achieve that, we provide a Printfable trait that's implemented only for types that our formatting macro accepts. Everything else will result in a compile-time error. See the docs in trait module for more on that.

Dependencies