Open
Description
I was slightly surprised to see that code like this:
pub fn foo() {
format!("Hello {}", 4);
}
does not get optimised to a nop. It still calls format!()
. This is surprising from a user point of view - you're calling a function, but it seems like it has no side effects and I don't use the result, so surely it should be removed?
I presume the reason it doesn't is that it allocates a String
and heap allocation is considered to be a side effect? Whatever the reason I think that it would be nice if it was optimised.
If you're wondering about motivation, consider a debug trait with a default implementation that does nothing. You could call debug.write(&format!(...))
and it would be fully optimised away for the default implementation.