News for hackers. Stuff that matters.
Re:Rust 1.47 by adamch on Thursday October 08, 2020 @10:04AM (#24719169)
It's a little thing, but one of my favourite parts of any Rust release is seeing which stdlib functions become `const`. The ergonomics of build-time evaluation are slowly improving, and I'm very excited to slowly do less at runtime!
Re:Rust 1.47 by scudd on Thursday October 08, 2020 @11:05AM (#24719740)
Can increase in use of `const fn`'s create a noticeable decrease in compile times? As someone whose never worked on compilers, I'm not really sure what the magnitude of that processing time is in relation to everything else.
Re:Rust 1.47 by jsheard on Thursday October 08, 2020 @11:11AM (#24719813)
The compiler would already evaluate most of this stuff at compile time, you just weren't allowed to rely on it until now.

e.g. char::is_ascii_alphabetic() was made const in 1.47, but 1.46 would already run it at compile time: https://godbolt.org/z/hWf65f

The difference is in 1.47 you can use the result to assign something that must be known at compile time.

Re:Rust 1.47 by pjmlp on Thursday October 08, 2020 @10:28AM (#24719352)
The problem with "const of all things" is that you will get enough free time to prepare a big meal.
Re:Rust 1.47 by jsheard on Thursday October 08, 2020 @10:41AM (#24719478)
In many cases the functions being constified were already evaluated during compilation where possible though, the compile time cost was already sunk.

Making them const just formally guarantees the compiler can evaluate them at compile time which makes them useful in more contexts.

Re:Rust 1.47 by pjmlp on Thursday October 08, 2020 @11:20AM (#24719916)
"constexpr of all things" kind of proves the point in compile times, and so far Rust isn't fairing much better in that regard.
Re:Rust 1.47 by ChrisSD on Thursday October 08, 2020 @11:40AM (#24720152)
I think the point is that rustc + llvm has long made everything "constexpr" (well as much as it can). After all it's a missed optimization if it fails to evaluate something at compile time if it can be so evaluated.

`const fn` is merely an API guarantee that the function can always be evaluated at compile time if the arguments are themselves known at compile time.

Re:Rust 1.47 by nwah1 on Thursday October 08, 2020 @09:24PM (#24725774)
Not a rust user. Could const be inferred instead of needing to be explicit? At least, hypothetically?
Re:Rust 1.47 by steveklabnik on Thursday October 08, 2020 @10:03PM (#24725996)
In theory, yes. In practice, it is an API commitment, and so it is not inferred, because you as a library author have to decide if you want to commit to it or not.
Re:Rust 1.47 by nwah1 on Thursday October 08, 2020 @11:21PM (#24726440)
Maybe for stuff that never can be externally accessed, const should be inferred as an optimization pass.
Re:Rust 1.47 by steveklabnik on Friday October 09, 2020 @08:38AM (#24729257)
Yes, this is already done. But that's not the more interesting part of the feature. Once you require something to be const, you can use it in contexts that require something to be const. Inferring it in non-const contexts is less powerful.
Re:Rust 1.47 by DC-3 on Thursday October 08, 2020 @01:08PM (#24721263)
Long compile times suck but long runtimes suck more.
Re:Rust 1.47 by cxr on Thursday October 08, 2020 @02:23PM (#24722101)
Counterpoint:

> When you have a small team that write tests and a compiler that is on the boundary of interactive speed, compiler speed dominates performance.

https://crawshaw.io/blog/fast-compilers

Re:Rust 1.47 by wtetzner on Monday October 12, 2020 @12:31PM (#24756172)
Rust is already not near the boundary of interactive speed.
Re:Rust 1.47 by cxr on Monday October 12, 2020 @05:24PM (#24759604)
Yes.
Re:Rust 1.47 by creata on Thursday October 08, 2020 @10:59AM (#24719673)
Can't the Rust compiler cache the evaluated outputs?
Re:Rust 1.47 by pjmlp on Thursday October 08, 2020 @11:21AM (#24719928)
In theory yes, with the current LLVM toolchain doesn't look like it.
Re:Rust 1.47 by vlang1dot0 on Thursday October 08, 2020 @11:32AM (#24720073)
LLVM has nothing to do with `const` evaluation in Rust. `const` evaluation happens at the MIR level which participates in incremental compilation.
Re:Rust 1.47 by pjmlp on Thursday October 08, 2020 @01:35PM (#24721558)
> cache the evaluated outputs

Was the point of my answer.

Re:Rust 1.47 by vlang1dot0 on Thursday October 08, 2020 @02:30PM (#24722182)
Apologies but as your comment currently stands, it reads to me like "in theory yes, but actually no".
Re:Rust 1.47 by k__ on Thursday October 08, 2020 @10:16AM (#24719253)
What does "becoming const" mean?
Re:Rust 1.47 by mden on Thursday October 08, 2020 @10:25AM (#24719331)
Const values are inlined at compile time wherever they are used.

Const functions let you run code to define const values. This code will be run at compile time before the const values are inlined.

For example let's say you have a constant for PI but you need 2*PI. You could do the multiplication in const context, meaning the multiplication will happen during compilation and not execution. Slowly more and more of the language is becoming usable in const context so more advanced things can be done during compile time.

Re:Rust 1.47 by colejohnson66 on Thursday October 08, 2020 @10:58AM (#24719667)
Well, if you need `2*PI`, there’s now `f32::TAU` and `f64::TAU` :) You’re still correct though
Re:Rust 1.47 by happythomist on Thursday October 08, 2020 @10:21AM (#24719283)
I believe it's similar to "constexpr" in C++ -- it means those functions can now be evaluated at compile time.
Re:Rust 1.47 by dahfizz on Thursday October 08, 2020 @10:25AM (#24719326)
https://doc.rust-lang.org/reference/items/functions.html#con...

Basically, const functions can be used in places regular functions can't because they are directly "run" by the compiler at compile time.

Re:Rust 1.47 by hinkley on Thursday October 08, 2020 @01:57PM (#24721809)
I like that const functions occupy a space between a normal function and hygienic macros. You can avoid writing out lookup tables long-form where the entries are very predictable. For instance you could write a base64 encoder and decoder without putting ["a", "b", "c"... ] in your code base, and then have to worry if you accidentally put "D" in there twice. The typical alternative in other languages is to evaluate this at startup time, which adds latency to your program.

Compile-time function evaluation is also one of the features of the Jai programming language. If you can't get people to use your language, getting them to steal all your features is a good consolation prize.

Re:Rust 1.47 by jsheard on Thursday October 08, 2020 @07:10PM (#24724940)
> If you can't get people to use your language, getting them to steal all your features is a good consolation prize.

It's difficult to use Jai as long as it only exists on Jonathan Blows computer, he still hasn't released it

Re:Rust 1.47 by Ygg2 on Thursday October 08, 2020 @10:20AM (#24719280)
Means that functions are evaluated at compile time.

However in this case, these are const generics, so you no longer need to use macro to generate [char;1],[char;2]...

You just write [char; N] where N is const. Compiler determines which values of N are used and generates for them an appropriate version.

Re:Rust 1.47 by steveklabnik on Thursday October 08, 2020 @10:23AM (#24719313)
can be, you can call them at runtime too. And to be clear, const generics aren't stable yet, they're being used as an implementation detail of the standard library here, you cannot yet use them on stable in your code.
Re:Rust 1.47 by Nullabillity on Thursday October 08, 2020 @10:24AM (#24719318)
> However in this case, these are const generics, so you no longer need to use macro to generate [char;1],[char;2]...

Const generics still aren't stable. I believe the GP was talking about the list of new `const fn`s in the release (near the bottom of the post).