Can Rust make languague more sugar

Use syntax comparison: https://www.programming-idioms.org/ Or collect a syntactic sugar, and then compare it with rust. Can you compare python ruby ​​kotlin scala c# and other languages ​​with more syntactic sugar, such as Kotlin's Optional Get the length of the string s?.length?:0 For example, Python gets the length of the string len(s)

Can Rust introduce more syntatic sugar? Yes. Should it be done? It has to be pondered on a case-by-case basis.

Do you have any proposal(s) that would benefit the language?

1 Like

What would be the reason for changing the retrieval of a string length to use special syntax?

Features and especially minor syntactic changes need a very strong motivation to get into the language, and the bar is even higher for syntax that special-cases types ot other language elements. One shouldn't make every function call have its own syntax just for the sake of adding new syntax. It would result in nothing but confusion.

6 Likes

The optional path case could be a bit cleaner: currently the plan is something like try { value?.path?.with?.options }, which is decent, and I wouldn't be against something like a ?? b being shorthand for a.unwrap_or_else(|| b)

2 Likes

The postfix ? operator already works with Option. As for ??, you still didn't mention any motivation. It's not any clearer than unwrap_or, quite the opposite.

1 Like

And rust gets it with s.len() -- one extra period in the rust version doesn't seem so horrible that it'd be worth adding special syntactic sugar for it.

If you prefer writing len(s), you could of course write

fn len(s: &str) -> usize { s.len() }

in your code to be able to do len("Hello").

(I would discourage it, personally, but you can if you want.)

1 Like

The relevant bit was try {} so you don't drop out of the function.

Having a nice sugar for delayed defaulting with unwrap_or_else() is hardly necessary, sure (it's still sugar, after all), but it would help guide away from too eager evaluation with a.unwrap_or(b) (notice that you just confused the two in your reply). I'm not strongly for it, but I think it's one of the better options for making the syntax a bit sweeter (assuming some valid syntax is found: ?? is already just two ?s)

1 Like

thanks a lot ervery one ,i need study rust a lot。 and python is so easy,that child can use it。i hope rust can be more popular that can have 【ten million pepole】 to use it

thanks a lot ! this what i want. Utils like guava Collections Lists, i shuold make and use some like that !

thanks a lot !!