7 releases

0.2.1 Feb 12, 2024
0.2.0 Feb 12, 2024
0.1.4 Sep 10, 2023
0.1.3 Sep 23, 2022
0.1.1 Aug 30, 2021

#87 in WebAssembly

Download history 1918/week @ 2024-01-19 3218/week @ 2024-01-26 4400/week @ 2024-02-02 4046/week @ 2024-02-09 3480/week @ 2024-02-16 4290/week @ 2024-02-23 2970/week @ 2024-03-01 3233/week @ 2024-03-08 3710/week @ 2024-03-15 2847/week @ 2024-03-22 4439/week @ 2024-03-29 4695/week @ 2024-04-05 4506/week @ 2024-04-12 5697/week @ 2024-04-19 5958/week @ 2024-04-26 4308/week @ 2024-05-03

21,477 downloads per month
Used in 8 crates (3 directly)

MIT license

30KB
640 lines

sprintf-rs

a clone of C sprintf in Rust

This crate was created out of a desire to provide C printf-style formatting in a WASM program, where there is no libc.

Note: You're probably better off using standard Rust string formatting instead of thie crate unless you specificaly need printf compatibility.

This crate implements a dynamically type-checked function vsprintf and macro sprintf!.

Usage example:

use sprintf::sprintf;
let s = sprintf!("%d + %d = %d\n", 3, 9, 3+9).unwrap();
assert_eq!(s, "3 + 9 = 12\n");

libc is a dev dependency as it is used in the tests to compare results. std is used for some maths functions.


lib.rs:

Libc s(n)printf clone written in Rust, so you can use printf-style formatting without a libc (e.g. in WebAssembly).

Note: You're probably better off using standard Rust string formatting instead of thie crate unless you specificaly need printf compatibility.

It follows the standard C semantics, except:

  • Locale-aware UNIX extensions (' and GNU’s I) are not supported.
  • %a/%A (hexadecimal floating point) are currently not implemented.
  • Length modifiers (h, l, etc.) are checked, but ignored. The passed type is used instead.

Usage example:

 use sprintf::sprintf;
 let s = sprintf!("%d + %d = %d\n", 3, 9, 3+9).unwrap();
 assert_eq!(s, "3 + 9 = 12\n");

The types of the arguments are checked at runtime.

Dependencies

~325–790KB
~19K SLoC