Live data from Hacker News

Rust for JavaScript Developers – Functions and Control Flow

sheshbabu.com

11–20 of 78 posts

Re: Rust for JavaScript Developers – Functions and Control Flow

#11
Doing currency manipulation using with i32 as an example for developers of a language that only supports float. What can go wrong?

Not only this is a bad idea from a programming perspective, or a teaching perspective, it is also a bad idea from a taxation perspective. In terms of taxation, what you suggest would result in [at least] civil penalties.

If you are going to create examples of code that manipulates currency, at least take it seriously and think about what would actually happen to people if they decided to use your code. Otherwise, use another concept to illustrate your example.

Re: Rust for JavaScript Developers – Functions and Control Flow

#12
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 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

Re: Rust for JavaScript Developers – Functions and Control Flow

#13

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…

Curious why didn't you use promises in the nodejs code?

Re: Rust for JavaScript Developers – Functions and Control Flow

#14

Doing currency manipulation using with i32 as an example for developers of a language that only supports float. What can go wrong? Not only this is a bad idea from a programming perspective, or a teaching perspective, it is also a bad idea from a taxation perspective. In terms of taxation, what you suggest would result in [at least] civil penalties. If you are going to create examples of code that manipulates currenc…

You're being harsh but yeah I kinda agree with you.

Re: Rust for JavaScript Developers – Functions and Control Flow

#15
post #13

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…

Curious why didn't you use promises in the nodejs code?

What part of their reply indicated they did not?

Re: Rust for JavaScript Developers – Functions and Control Flow

#16
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. Plus if the else clause has a side effect, you can't assign it to the variable, which means you need to instantiate to null or a dummy value. Very messy.

What's funny is that expression oriented languages translate really nicely to stack VMs. My hobby language compiles down to WASM and it was almost trivial to code gen if expressions.

Re: Rust for JavaScript Developers – Functions and Control Flow

#18

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.

Re: Rust for JavaScript Developers – Functions and Control Flow

#19

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.

No, if expressions can basically only be used with else, otherwise you have a type mismatch

Re: Rust for JavaScript Developers – Functions and Control Flow

#20

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…

It might be interesting to compare them if you'd care to share.
Post reply on HN