6 ms·
I don't get why they'll let the language be abstracted in that way, but are against having ternary operators. It's the one thing that's somewhat bugged me about
by QuantumLogic 10y ago
I don't get why they'll let the language be abstracted in that way, but are against having ternary operators. It's the one thing that's somewhat bugged me about Rust. Aside from this one minor thing, I"ve been really loving the language itself.
let greater = a < b ? b : a;
Is a lot better than having to write this all the time:
let greater = if a < b { return a; } else { return b; };
- steveklabnik 10y agoThis doesn't duplicate anything in the language; it moves a stdlib macro to be included in the core language. Since if is an expression, ternay operators would be wholly redundant.
- QuantumLogic 10y agoThe whole point of it would be to have a higher level of abstraction which makes codebases more concise. Avoiding duplication for the sake of avoiding it is as nonsensical as avoiding 100 dollar bills since we already have 100 single dollar bills that could make it up.
- masklinn 10y ago> The whole point of it would be to have a higher level of abstraction What are you talking about? A ternary isn't a higher level of abstraction, it's at best the exact same thing with a different syntax, at worst it's a more limited version of the same.
- masklinn 10y agoBut…. these are two completely different statements, the equivalent Rust is if a < b { b } else { a } why are you adding sprurious returns? And if you need to get the minimum of two values so much that actually impacts you, write a `min!` macro?
- shepmaster 10y agoNo macros needed! use std::cmp::min; min(a, b); https://doc.rust-lang.org/std/cmp/fn.min.html https://doc.rust-lang.org/std/cmp/fn.min.html
- masklinn 10y agoHa. I should have known, thanks.
- sixbrx 10y agoDitch the returns and semicolons in the latter, then if is your ternary op, just more readable in cases where a and b are expressions more complex than in this example.
- Gankro 10y agoI tip my hat to you for remaining committed to one of the oldest Rust trolls there is.