I wonder if quote of the week can be used to bust some myth.
But what's the point of a const function then?
It makes code like this compile:
const FOO: i32 = my_const_fn(13);
That code is rejected unless
That is the only effect
fn. And AFAIK we never say anything else anywhere in the docs, so I do not comprehend where all these extra assumptions come from that you have been making. I'd really like to know as I'd like to avoid people making such false assumptions.
I always thought that const means all of these happens in compile time and none of it goes to runtime except those called in non-const places.
Code in const "places" (usually called const contexts) must run at compiletime. So this statement is true but I don't think it means what you think it means. In particular,
formatis impossible to use in const contexts:
const FOO: String = format!("abc"); // does not compile
format!can only be used in non-const-contexts, that means it never happens at compiletime. Or rather, it is never guaranteed to happen at compiletime -- optimizations can do whatever they want, but there's no guarantee they ever happen.
-- @RalfJung github comments https://github.com/rust-lang/rust/pull/75894#discussion_r477192216