Converting f32 to string has inconsistent behavior in debug and display mode

Try the codes below in the playground:

fn main() {
    let num = 1.0
    println!("{}", num);
    println!("{:?}", num);
}

You'll got the output as below:

1
1.0

I guess it makes more sense if they have the same results.

Would anyone like to share your idea here?

What does this have to do with Vector? This is Debug vs Display for f32. These are implemented differently for f32.

9 Likes

Thanks, you are right, I should have described the question as Converting f32 to string has inconsistent behavior in debug and display mode.

Debug and Display serve different purposes, so it is quite expected that they can have different output. If they always had the same output, it would be rather redundant to have both, wouldn't it?

As another example, consider

fn main() {
    let msg = "Hello\nWorld";
    println!("{}", msg);
    println!("{:?}", msg);
}

This prints

Hello
World
"Hello\nWorld"
1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.