Live data from Hacker News

Such a Little Thing: The Semicolon in Rust

lucumr.pocoo.org

1–10 of 81 posts

Re: Such a Little Thing: The Semicolon in Rust

#2
I memorably referred to Rust's significant semicolons as "the worst thing in the language" after my very first read through the tutorial, last November.

Almost a year later, I'm just as in love with Rust's semicolon rules as Armin is. However, I bet new users will still be just as instinctively revolted as I initially was.

Re: Such a Little Thing: The Semicolon in Rust

#5
Is that really some "special behavior"? As far as i understand the semicolon is just an expression separator like in Erlang and Pascal. In contrast, the semicolon is a statement terminator in C. C uses the comma for expression separation. Practically everything is an expression in Rust, even blocks, so

  foo()       => evaluates to the return value of foo
  { foo() }   => evaluates to the return value of foo
  { foo(); }  => evaluates to nil
The last case is not a special case. The last expression in the block determines the returned value. There is an expression separator in there, so there must be two expressions which are separated by it. The first one is foo() and second one is ... wait for it ... the empty expression. Which value should EmptyExpression have? Of course: nil, which would be called void in C-land.

Re: Such a Little Thing: The Semicolon in Rust

#7
post #5

Is that really some "special behavior"? As far as i understand the semicolon is just an expression separator like in Erlang and Pascal. In contrast, the semicolon is a statement terminator in C. C uses the comma for expression separation. Practically everything is an expression in Rust, even blocks, so foo() => evaluates to the return value of foo { foo() } => evaluates to the return value of foo { foo(); } => evalua…

There was some discussion about doing this in D, and there was some in C++11, too. I argued that the presence or absence of the ; did not visually stand out very well, and so would be a source of confusion and errors.

Hence, ; remained as a statement terminator, and the return keyword served to indicate returning an expression.

However, the D lambda syntax does not require a ; when the lambda body consists of only a single expression:

http://dlang.org/expression.html#Lambda

and in practice this has turned out to be well liked.

Re: Such a Little Thing: The Semicolon in Rust

#8
post #5

Is that really some "special behavior"? As far as i understand the semicolon is just an expression separator like in Erlang and Pascal. In contrast, the semicolon is a statement terminator in C. C uses the comma for expression separation. Practically everything is an expression in Rust, even blocks, so foo() => evaluates to the return value of foo { foo() } => evaluates to the return value of foo { foo(); } => evalua…

It's odd because people think of semicolons as the end of a statement, not a delimiter between statements.

Re: Such a Little Thing: The Semicolon in Rust

#9

    The downside is that you would have to put () (Rust's version of “nil”) in a bunch of functions to fulfil the requirements of the callback's signature since otherwise the type inferred from the function would be the value of the last expression
Wouldn't co/contra-variance solve this entirely? It works just fine in Scala for example

    scala> def runTwice(f: () => Unit) = {
         |  f()
         |  f()
         | }
    runTwice: (f: () => Unit)Unit

    scala> runTwice{ () =>
         |  println("moo")
         |  1
         | }
    moo
    moo
Note how it expects a function that returns Unit, i'm passing in a function that returns an Int (1), but the compiler is perfectly happy.

Re: Such a Little Thing: The Semicolon in Rust

#10

Are semicolons annoying to type? Probably, I got used to them The semicolon is the least-annoying non-letter character to type. It's right there on your home row.

Hello American. I'm from one of those other pesky countries that make up most of the human population of earth, and which usually have other keyboard layouts. I have to type shift+, to get the semicolon.
Post reply on HN