|
|
Subscribe / Log in / New account

Ternary expression?

Ternary expression?

Posted Oct 1, 2024 6:38 UTC (Tue) by milian.wolff (guest, #153128)
In reply to: Ternary expression? by Paf
Parent article: Coccinelle for Rust

There is no semantic difference between these two statements. Rust simply has no ternary expressions. Instead it has first-class support for expressions in many more contexts compared to C and C++, allowing for whole inline if / else if / else cascades or even loops etc.


to post comments

Ternary expression?

Posted Oct 1, 2024 13:34 UTC (Tue) by Paf (subscriber, #91811) [Link] (3 responses)

Ahh, this comment helps me understand, thank you! There is full support for control flow code in (to a C person) fairly arbitrary spots.

So the particular example is perhaps poorly chosen as it has an extremely close equivalent in C.

Ternary expression?

Posted Oct 1, 2024 13:44 UTC (Tue) by daroc (editor, #160859) [Link] (2 responses)

That's fair — it's the example Roy chose, and he is the expert in this case, so I assume it was the best example available. Maybe it would help to point out that in Rust you can have things like while loops inside if expressions, which isn't really possible with C ternary expressions. So you need generalized control flow inside expressions, instead of a relatively small subset.

Ternary expression?

Posted Oct 2, 2024 11:29 UTC (Wed) by khim (subscriber, #9252) [Link]

> Maybe it would help to point out that in Rust you can have things like while loops inside if expressions, which isn't really possible with C ternary expressions.

We are not talking arbitrary C code, though, we are talking about kernel. In Linux kernel, of course, it's not just possible, it's routine, there are many macros that exploit that ability.

Ternary expression?

Posted Oct 3, 2024 14:16 UTC (Thu) by Paf (subscriber, #91811) [Link]

Thank you! (It didn’t sound like it was your choice of example, fwiw)


Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds