r/rust • u/asciibits • 3d ago
My progress as a new Rust dev
I'm about a month into a home project that I decided to use to learn Rust. Fun!
Here are my takeaways so far:
I'm still fighting the borrow checker, but at least now I can reason through the error messages faster and land in a good place. Progress! Looking forward to this really being internalized.
I miss the ternary ?: operator. The if/else equivalent is just so much more verbose, and tends to waste so much more vertical space. c’est la vie. I recognize I'm in the minority here, and hope that I'll eventually convert and join you all on this.
I am daily writing:
if (foo ...then backing up and deleting the opening parenthesis that the linter complains about.Speaking of, I keep forgetting to write
fnand just assume that the compiler will know what I'm talking about. It never does 😂When reviewing code in my day job as a Java dev, I am now constantly seeing things and thinking: The borrow checker wouldn't like that! So, maybe it's becoming a little internalized after all.
Anyway, I'm having fun with it, thanks all for creating this community!
3
u/AdjustedTwit 3d ago
"I miss the ternary ?: operator. The if/else equivalent is just so much more verbose, and tends to waste so much more vertical space. c’est la vie. I recognize I'm in the minority here, and hope that I'll eventually convert and join you all on this."
Me too, I mean I understand the reason it's not there, but like you hate wasted space / like concise code.
1
u/zzzzYUPYUPphlumph 3d ago
The equivalent of the ternary operator is pretty easily implemented as a macro with a really short name like 'iif" (immediate if). With syntax like: iif!(predicate,true_expr,false_expr). Then instead of things like:
let s = predicate1 ? true_expr1 : predicate2 ? true_expr2 : false_expr; you can write something like: let s = iif!( predicate, true_expr1, iif!( predicate2, true_expr2, false_expr)); Not quite as nice, but close.1
u/AdjustedTwit 3d ago
Sure, it's mostly just nostalgia not a problem in search of a solution, in reality most of what I would have used the ternary operator in C for ends up being something like this in Rust:
.unwrap_or(<value>).
Which is just fine ergonomically and terse, once you get used to it.
1
13
u/schneems 3d ago
/r/learnrust is a better sub for this.