4 ms·
I wonder why && and || are not allowed in const functions? > All boolean operators except for && and || which are banned since they are short-circuiting. I gu
by marricks 6y ago
I wonder why && and || are not allowed in const functions?
> All boolean operators except for && and || which are banned since they are short-circuiting.
I guess I'm missing something obvious but why does the short circuiting break const-ness?
- Rusky 6y agoThat was a restriction from before 1.46 stabilized control flow in const functions. Now that we have worked out the details around `if`, we can also stabilize `&&` and `||`. (I'm a little surprised they weren't stabilized at the same time! Edit: they were! I just didn't look closely enough.)
- est31 6y ago> (I'm a little surprised they weren't stabilized at the same time!) They were stabilized at the same time, see the release announcement. https://blog.rust-lang.org/2020/08/27/Rust-1.46.0.html https://blog.rust-lang.org/2020/08/27/Rust-1.46.0.html
- phire 6y agoShort circuiting introduces conditional branching. If you call a function on the right hand side of a || or && it might or might not be executed depending on the value of the left hand side. Until this version of rust, all conditional branches were banned from const functions. I guess to keep things simple they just banned any feature that might cause branching.
- marricks 6y agoAhh that makes a lot of sense, if you're going to have a compiler insert the result of a function having conditional branching seems a bit gnarly I guess?
- marton78 6y agoWhy is branching a problem? Since `if` is now enabled in a const fn, it's trivial to rewrite && with & and if.
- kibwen 6y ago`if` wasn't enabled before, so `&&` wasn't enabled. Now that `if` is enabled, `&&` now works too.
- lenkite 6y agoThat's a weird restriction on not allowing logical operators. AFAIK C++ allows this for constexpr functions - as long as it can be evaluated at compile time.
- deleted 6y ago[deleted]