More syntax sugar

I come from c# world, wondering if we can have more syntax sugar. The syntax sugars I miss the most in c# are:

  1. var x = expra ? exprb :exprc; a simple if else expression retuen values.
  2. dealing with null: var x = a?.pa?.pb?.pc ?? pp; i.e., if anything before is null, just asign pp to x, or else. it's a.pa.pb.pc. I know we don't have null in Rust, but we have ton of Result and Option, which is quite tedious some time.
  3. Linq: GOD, if we can integrate Linq to Rust, that'll make a lot people happy to work with database in Rust.

Rust’s normal ifs already have return values, so this is valid today:

let x = if expra { exprb } else { exprc };

Rust’s version of this is the ? operator (stable) and try blocks, which are not yet stabilized. Once they are, you should be able to write something like this:

let x = try { a?.pa?.pb?.pc? }.unwrap_or(pp);

NB: This code might not work on unstable today due to a type inference failure


Database access is a much higher-level concept than is likely to go into core Rust, but procedural macros allow this sort of extension to be added in a library.

3 Likes

Generally, no, not without a very strong motivation. Rust has macros. If you want some particular piece of syntax, then write a macro. Adapt to the language – don't expect the language to adapt to your personal taste.

if is already an expression, and so are almost all executable constructs in Rust.

The ? operator already works with Option.

I strongly disagree. The problem with working with databases is not the lack of some superficial syntactic sugar. Writing select foo from Bar is not any easier than Bar::all().map(|b| b.foo).

The problem is at the semantic level – the long-term retention of information, correctly handling atomicity and transactions, the volume of data to be stored, the need for efficient indexing, the lack of ability to represent a direct equivalent of pointers and recursive traversal of linked structures are far more serious problems (and active areas of research — my PhD work is all about writing a type-safe, capable, seamless database abstraction layer for Rust). If you want easier interaction with databases, use an ORM for now.

6 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.