Live data from Hacker News

Rust for JavaScript Developers – Functions and Control Flow

sheshbabu.com

21–30 of 78 posts

Re: Rust for JavaScript Developers – Functions and Control Flow

#21
post #7

Earlier quoted context omitted.

Speaking of which, coming from Rust (well, when I say it like that it sounds like Rust is the only language I ever used which is of course not true. I think Rust as a first language would be highly unusual still), I enjoyed being able to say Rust code: let foo = if x 50 { 9000 } else { 42 }; And I miss this in Swift. The ternary operator is fine on its own but I’m not a huge fan of nested ternary operators. With a bi…

I find ternary operators perfectly readable if you split the parts across lines — they map exactly to the conditions and statements in an if-else group. Cond1 // if this ? Result1 // return this : cond2 // else if this ? Result2 // return this : cond3 // else if this ? Result3 // return this : ResultElse // else return this

Which is not true in PHP up until the most recent version (iirc) due to it being left associative instead of right associative you are assuming.

Re: Rust for JavaScript Developers – Functions and Control Flow

#22
post #7

Earlier quoted context omitted.

Speaking of which, coming from Rust (well, when I say it like that it sounds like Rust is the only language I ever used which is of course not true. I think Rust as a first language would be highly unusual still), I enjoyed being able to say Rust code: let foo = if x 50 { 9000 } else { 42 }; And I miss this in Swift. The ternary operator is fine on its own but I’m not a huge fan of nested ternary operators. With a bi…

I find ternary operators perfectly readable if you split the parts across lines — they map exactly to the conditions and statements in an if-else group. Cond1 // if this ? Result1 // return this : cond2 // else if this ? Result2 // return this : cond3 // else if this ? Result3 // return this : ResultElse // else return this

True, or even:

    Cond1 // if this
      ? Result1 // return this
      : cond2 // else if this
        ? Result2 // return this
        : cond3 // else if this
          ? Result3 // return this
          : ResultElse // else return this

Re: Rust for JavaScript Developers – Functions and Control Flow

#23

It'd be nice to mention that Rust has if expressions, not if statements. Therefore the example could be simplified to: fn calculate_tax(income: i32) -> i32 { if income = 10 && income

I don't know Rust - but is it possible to write it like this? fn calculate_tax(income: i32) -> i32 { if income on edit: tried to fix formatting but didn't work.

It wouldn't work like that but if you made them else if and else branches respectively then yes it would. Each if/if else/else block as a whole is a single statement. But two if statements and a literal is three individual statements

Re: Rust for JavaScript Developers – Functions and Control Flow

#24

It'd be nice to mention that Rust has if expressions, not if statements. Therefore the example could be simplified to: fn calculate_tax(income: i32) -> i32 { if income = 10 && income

Rust can also match on ranges which is perhaps the most idiomatic form for this.

Re: Rust for JavaScript Developers – Functions and Control Flow

#25
post #7

It'd be nice to mention that Rust has if expressions, not if statements. Therefore the example could be simplified to: fn calculate_tax(income: i32) -> i32 { if income = 10 && income

Speaking of which, coming from Rust (well, when I say it like that it sounds like Rust is the only language I ever used which is of course not true. I think Rust as a first language would be highly unusual still), I enjoyed being able to say Rust code: let foo = if x 50 { 9000 } else { 42 }; And I miss this in Swift. The ternary operator is fine on its own but I’m not a huge fan of nested ternary operators. With a bi…

I dislike the ternary operator in languages like C++. And many languages copy-paste it because they are used to it. I much prefer the more literal and reversed form in Python.

Instead of:

int salary = isEmployed() ? 2000 : 0

I prefer:

int salary = 2000 if isEmployed() else 0

Re: Rust for JavaScript Developers – Functions and Control Flow

#26

It'd be nice to mention that Rust has if expressions, not if statements. Therefore the example could be simplified to: fn calculate_tax(income: i32) -> i32 { if income = 10 && income

I don't know Rust - but is it possible to write it like this? fn calculate_tax(income: i32) -> i32 { if income on edit: tried to fix formatting but didn't work.

If you wanted to list out cases without having to use `if`, `else if`, you'd idiomatically use match:

    fn calculate_tax(income: i32) -> i32 {
        match income {
            0..10 => 0,
            10..50 => 20,
            _ => 50,
        }
    }
See this playground for a couple other alternatives using match too https://play.rust-lang.org/?version=nightly&mode=debug&editi...

As others explained, you do need a default case (either an 'else' in your if statement, or an exhaustive match), otherwise how would the expression typecheck?

For a concrete example, if you use an 'if' only, you can have the following:

    let x = if income 
At that point, there's no way to statically know x's type. If income is >= 10, x is what?

In order to convince the compiler x has a statically knowable type, the "if" expression need an else statement, or should be converted into a match that the compiler can prove is exhaustive.

Re: Rust for JavaScript Developers – Functions and Control Flow

#27

I just converted a relatively simple node.js program to Rust (because the native libusb bindings for node.js crash immediately on Windows 7 64-bit) Rust made me worry about a lot more than I wanted to. Threads, locking, mutability. What was 80 lines of: 1. open USB device, get handle 2. start a WebSocket server, accept clients 3. proxy incoming USB device traffic to all WebSocket clients 4. proxy incoming WebSocket t…

I have a fair bit of Rust experience, and I agree that it adds a lot of cognitive overhead. It gets better over time as you learn how to appease the compiler, but the borrow checker can still be a pain in the ass. It's something you're constantly aware of, taking up space in your brain.

I can understand the move from C or C++ to Rust, the safety guarantees can be compelling. And it does feel like a much more modern language. However, if something with GC like C# can get the job done, I'd choose that every time over Rust.

Re: Rust for JavaScript Developers – Functions and Control Flow

#28
post #26

Earlier quoted context omitted.

I don't know Rust - but is it possible to write it like this? fn calculate_tax(income: i32) -> i32 { if income on edit: tried to fix formatting but didn't work.

If you wanted to list out cases without having to use `if`, `else if`, you'd idiomatically use match: fn calculate_tax(income: i32) -> i32 { match income { 0..10 => 0, 10..50 => 20, _ => 50, } } See this playground for a couple other alternatives using match too https://play.rust-lang.org/?version=nightly&mode=debug&editi... As others explained, you do need a default case (either an 'else' in your if statement, or an…

Nitpick: I'm pretty sure that "11..50" should be a "10..50".

Re: Rust for JavaScript Developers – Functions and Control Flow

#29
post #26

Earlier quoted context omitted.

If you wanted to list out cases without having to use `if`, `else if`, you'd idiomatically use match: fn calculate_tax(income: i32) -> i32 { match income { 0..10 => 0, 10..50 => 20, _ => 50, } } See this playground for a couple other alternatives using match too https://play.rust-lang.org/?version=nightly&mode=debug&editi... As others explained, you do need a default case (either an 'else' in your if statement, or an…

Nitpick: I'm pretty sure that "11..50" should be a "10..50".

Edited, yeah. Off-by-one errors, can't live without em.

Re: Rust for JavaScript Developers – Functions and Control Flow

#30
post #7

It'd be nice to mention that Rust has if expressions, not if statements. Therefore the example could be simplified to: fn calculate_tax(income: i32) -> i32 { if income = 10 && income

Speaking of which, coming from Rust (well, when I say it like that it sounds like Rust is the only language I ever used which is of course not true. I think Rust as a first language would be highly unusual still), I enjoyed being able to say Rust code: let foo = if x 50 { 9000 } else { 42 }; And I miss this in Swift. The ternary operator is fine on its own but I’m not a huge fan of nested ternary operators. With a bi…

Agreed about missing if and switch expressions in Swift. My preferred way of writing that would be:

    let foo: Int
    if x  50 {
        foo = 9000
    } else {
        foo = 42
    }
That way you get immutable foo and the compiler will shout at you if you forget to assign the value in one branch. It's not quite as nice as an expression, but it's less punctuation than the closure and the result is just as safe.
Post reply on HN