Evaluation order of boolean subconditions

So as a bit of background I'm working on a code coverage tool for rust and am currently tackling condition coverage which involves working out if every boolean subcondition has been evaluated as true and false - so for (A || B) && C. A, B and C have to have been true and false for 100% coverage.

Now in C and C++, the evaluation order of boolean subconditions isn't standardised and could potentially change given different optimisation flags for the compiler. Which can cause things to go wrong if people have subconditions with side effects. Does rust make any guarantees about execution order? In this example I made in rust playground the first condition is always executed first Rust Playground I'm just wondering if this is guaranteed?

Thanks for any help, I'm aware this is a bit language lawyery so help is greatly appreciated :slight_smile:

1 Like

Is that really true? I found this which says that can only change if the ops are overloaded;

(But you can't change && or || in Rust at all.)

Maybe it isn't true then, at least not in recent standards. I guess I assumed based on MISRA C rules and vague memories of points raised here c++ - Undefined behavior and sequence points - Stack Overflow

I suppose that UB leads to an extension of what if there is a condition f1(&mut x) || f2(&mut x) where f1 and f2 both have side effects, because it looks like that would be UB in C++.

There's a sequence point after logical operators, so even side effects should be safe.