Hacker News new | ask | show | jobs
by oguz-ismail 651 days ago
The final version is still ugly. Why `pub fn'? Why is public not the default and why do you have to specify that it's a function? Why `: type' and `-> type', why can't type go before the identifier? Why do you need `File::' and `Bytes::'? What is that question mark? Why does the last statement not need a semicolon? It's like the opposite of everything people are used to.
4 comments

> Why `pub fn'?

I prefer it this way; defaults should be conservative or more common: I write many many more private functions than public. I'm not sure what your objection to 'fn' is... seems like a superficial problem. It likely makes the language easier for the compiler to parse, and to me, it makes it easier to read.

> Why `: type' and `-> type', why can't type go before the identifier?

Because putting the type afterward is more ergonomic. If you're used to C/C++/Java/etc. it feels weird, but once you start writing code with the type after the declaration, it feels much more natural.

> Why do you need `File::' and `Bytes::'?

I'm not sure what you mean here. They're types. You have to specify types.

> What is that question mark?

The question mark is basically "if the Result is Ok, unwrap it; if it's an Err, return it immediately."

> Why does the last statement not need a semicolon?

Leaving off the semicolon returns the last expression from the block.

> It's like the opposite of everything people are used to.

Maybe if your experience with programming languages is fairly limited...

Short answer for the type ordering and `fn`: because C/C++/Java tried that type of syntax and the result was an ambiguous grammar that is way too hard to parse, not to mention C's overly complicated pointer syntax.
As someone who doesn't think it is pretty, but knows Rust I went through all your points and let me assure you except for the one where you wonder why the syntax can't be more like C/C++ where it comes down to taste, all of your questions have an answer that really makes sense if you understand the language.

E.g. making pub default is precisely the decision a language would make that values concise code over what the code actually does.

Definitely agree, “pub” was one of the design decisions I loved learning Rust. If you forget to add it, you’ll get a compiler error. But if pub was default, I’d be exposing code unnecessarily. And no need for a separate private keyword, the absence of pub is sufficient.

The same reasoning works for “mut” as well.

That said, I don’t like Rust’s syntax. Especially once you get to lambdas, things get hard to read.

Your points have nothing to do with ugliness.

> Why `pub fn'? Why is public not the default and why do you have to specify that it's a function?

If public were the default, you'd end up having to make other functions `priv fn` instead.

> Why `: type' and `-> type', why can't type go before the identifier?

It's easier to parse, and most major typed languages other than C/C++/C#/Java put the type after the identifier.

> Why do you need `File::' and `Bytes::'?

Seriously?

> What is that question mark?

The final version doesn't use a question mark.

> Why does the last statement not need a semicolon?

This is a legitimate question. In Rust, the last statement without a semicolon becomes the return value.

> If public were the default, you'd end up having to make other functions `priv fn` instead.

My guilty pleasure is Go's visibility system, where all functions that start with lowercase are private to the scope of the current class/file and all Functions that start with Uppercase are public.

It doesn't look but it would work and it's a mess when you need acronyms, but it somehow works great and the result looks nice.

I actually really dislike Go's system! Casing, to me, shouldn't be significant. (Just like I dislike how whitespace is significant in Python.) And to me it just doesn't look nice, but I also prefer snake_case over camelCase and PascalCase.