README.md
1itoa
2====
3
4[![Build Status](https://api.travis-ci.org/dtolnay/itoa.svg?branch=master)](https://travis-ci.org/dtolnay/itoa)
5[![Latest Version](https://img.shields.io/crates/v/itoa.svg)](https://crates.io/crates/itoa)
6[![Rust Documentation](https://img.shields.io/badge/api-rustdoc-blue.svg)](https://docs.rs/itoa)
7
8This crate provides fast functions for printing integer primitives to an
9[`io::Write`] or a [`fmt::Write`]. The implementation comes straight from
10[libcore] but avoids the performance penalty of going through
11[`fmt::Formatter`].
12
13See also [`dtoa`] for printing floating point primitives.
14
15*Version requirement: rustc 1.0+*
16
17[`io::Write`]: https://doc.rust-lang.org/std/io/trait.Write.html
18[`fmt::Write`]: https://doc.rust-lang.org/core/fmt/trait.Write.html
19[libcore]: https://github.com/rust-lang/rust/blob/b8214dc6c6fc20d0a660fb5700dca9ebf51ebe89/src/libcore/fmt/num.rs#L201-L254
20[`fmt::Formatter`]: https://doc.rust-lang.org/std/fmt/struct.Formatter.html
21[`dtoa`]: https://github.com/dtolnay/dtoa
22
23```toml
24[dependencies]
25itoa = "0.4"
26```
27
28<br>
29
30## Performance (lower is better)
31
32![performance](https://raw.githubusercontent.com/dtolnay/itoa/master/performance.png)
33
34<br>
35
36## Examples
37
38```rust
39use std::{fmt, io};
40
41fn demo_itoa_write() -> io::Result<()> {
42 // Write to a vector or other io::Write.
43 let mut buf = Vec::new();
44 itoa::write(&mut buf, 128u64)?;
45 println!("{:?}", buf);
46
47 // Write to a stack buffer.
48 let mut bytes = [0u8; 20];
49 let n = itoa::write(&mut bytes[..], 128u64)?;
50 println!("{:?}", &bytes[..n]);
51
52 Ok(())
53}
54
55fn demo_itoa_fmt() -> fmt::Result {
56 // Write to a string.
57 let mut s = String::new();
58 itoa::fmt(&mut s, 128u64)?;
59 println!("{}", s);
60
61 Ok(())
62}
63```
64
65The function signatures are:
66
67```rust
68fn write<W: io::Write, V: itoa::Integer>(writer: W, value: V) -> io::Result<usize>;
69
70fn fmt<W: fmt::Write, V: itoa::Integer>(writer: W, value: V) -> fmt::Result;
71```
72
73where `itoa::Integer` is implemented for i8, u8, i16, u16, i32, u32, i64, u64,
74i128, u128, isize and usize. 128-bit integer support requires rustc 1.26+ and
75the `i128` feature of this crate enabled.
76
77The `write` function is only available when the `std` feature is enabled
78(default is enabled). The return value gives the number of bytes written.
79
80<br>
81
82#### License
83
84<sup>
85Licensed under either of <a href="LICENSE-APACHE">Apache License, Version
862.0</a> or <a href="LICENSE-MIT">MIT license</a> at your option.
87</sup>
88
89<br>
90
91<sub>
92Unless you explicitly state otherwise, any contribution intentionally submitted
93for inclusion in this crate by you, as defined in the Apache-2.0 license, shall
94be dual licensed as above, without any additional terms or conditions.
95</sub>
96