9 releases

0.3.1 Jul 14, 2024
0.3.0 May 31, 2024
0.2.1 Feb 12, 2024
0.1.4 Sep 10, 2023
0.1.1 Aug 30, 2021

#60 in WebAssembly

Download history 4965/week @ 2024-07-29 3983/week @ 2024-08-05 3849/week @ 2024-08-12 2744/week @ 2024-08-19 3587/week @ 2024-08-26 2827/week @ 2024-09-02 2997/week @ 2024-09-09 3197/week @ 2024-09-16 3161/week @ 2024-09-23 3618/week @ 2024-09-30 3718/week @ 2024-10-07 4323/week @ 2024-10-14 4777/week @ 2024-10-21 4760/week @ 2024-10-28 4009/week @ 2024-11-04 4885/week @ 2024-11-11

18,609 downloads per month
Used in 11 crates (4 directly)

MIT license

32KB
705 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 this 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

~255–720KB
~17K SLoC