Macro dtoa

Source
macro_rules! dtoa {
    ($into_dtoa:expr) => { ... };
}
Expand description

Quickly format a float to a &str

This creates a DtoaTmp from an IntoDtoa, returns the output &str, and immediately goes out of scope.

The function signature would look something like:

fn dtoa<N: IntoDtoa>(num: N) -> &'tmp str
where
    'tmp: FreedAtEndOfStatement

DtoaTmp is created and immediately dropped, thus it cannot be stored:

let x = dtoa!(1.0);
        ^^^^^^^^^^- temporary value is freed at the end of this statement

assert_eq!(x, "1.0");
-------------------- compile error: borrow later used here

You must use the &str in 1 single statement:

assert_eq!(dtoa!(1.0), "1.0"); // ok

if dtoa!(f32::NAN) == "NaN" {
    // ok
}

// ok
let string: String = dtoa!(f32::INFINITY).to_string();
assert_eq!(string, "inf");

The macro expands to DtoaTmp::new().format(x):

// These are the same.

dtoa!(1.0);

readable::toa::DtoaTmp::new().format(1.0);