|
|
Subscribe / Log in / New account

Ternary expression?

Ternary expression?

Posted Sep 30, 2024 23:29 UTC (Mon) by Paf (subscriber, #91811)
Parent article: Coccinelle for Rust

I don’t know rust well at all, but how is this:
println!("{}", if boolean { "string 1" } else { "string 2" })

Different from:
printf(“%s”, boolean ? String 1 : string 2)
?


to post comments

Ternary expression?

Posted Oct 1, 2024 6:38 UTC (Tue) by milian.wolff (guest, #153128) [Link] (4 responses)

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.

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)

Ternary expression?

Posted Oct 1, 2024 8:38 UTC (Tue) by danielthompson (subscriber, #97243) [Link]

In C the ternary operator is an expression but control flow (such as if) are not expressions. In Rust control flow is "just an expression".

I suspect part of the concern is also that, by allowing coccinelle to recognise the if in the println!() macro, will also result in it having to test many, many other control flow expressions for matches which would have been ignored when they appear in C.

Ternary expression?

Posted Oct 1, 2024 11:48 UTC (Tue) by kaesaecracker (subscriber, #126447) [Link]

Rust does not have ternary expressions. You can use an if expression instead, which is equivalent.


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