4 releases

0.1.3 Sep 23, 2022
0.1.2 Nov 6, 2021
0.1.1 Aug 30, 2021
0.1.0 Aug 24, 2021

#161 in WebAssembly

Download history 292/week @ 2022-12-05 357/week @ 2022-12-12 261/week @ 2022-12-19 155/week @ 2022-12-26 303/week @ 2023-01-02 410/week @ 2023-01-09 257/week @ 2023-01-16 404/week @ 2023-01-23 474/week @ 2023-01-30 355/week @ 2023-02-06 489/week @ 2023-02-13 338/week @ 2023-02-20 343/week @ 2023-02-27 407/week @ 2023-03-06 480/week @ 2023-03-13 376/week @ 2023-03-20

1,614 downloads per month
Used in 4 crates (via openjp2)

MIT license

28KB
630 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.

No runtime deps