Rust for JavaScript Developers – Functions and Control Flow
1–10 of 78 posts
Re: Rust for JavaScript Developers – Functions and Control Flow
#2Therefore the example could be simplified to:
fn calculate_tax(income: i32) -> i32 {
if income = 10 && income Re: Rust for JavaScript Developers – Functions and Control Flow
#3It'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
Re: Rust for JavaScript Developers – Functions and Control Flow
#4Re: Rust for JavaScript Developers – Functions and Control Flow
#5Could someone explain why there is no dereference in let double = |n: &i32| -> i32 { n * 2 };
:)
Edit: On hindsight, I should've picked a simpler example that doesn't deal with references.
Re: Rust for JavaScript Developers – Functions and Control Flow
#6It'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
Re: Rust for JavaScript Developers – Functions and Control Flow
#7It'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 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 bit of whitespace it's somewhat readable still but still not as comfortable and easy to read correctly.
Swift code:
let foo = x 50 ? 9000 :
42 )
One way of writing something that is more similar to the way I'd write it in Rust would be to make a closure and then run it.Swift code:
let foo =
{ () -> Int in
if x 50
{
return 9000
}
else
{
return 42
}
}()
But it's a bit annoying still, both to read and to write. Also, even in the current version of Swift (Swift 5), the compiler cannot infer the return type of the closure on its own even though all of the branches return an Int, so I'd have to explicitly annotate that as I have done in the code above.I guess for a lot of people they would just make foo mutable and write the code as
Swift code:
var foo = 42
if x 50
{
foo = 9000
}
I concede that this is probably the most readable out of all of the three Swift code samples in my comment. But the point is that I didn't want foo to be mutable. I wanted to assign a value to it based on some simple logic and have it be immutable.Re: Rust for JavaScript Developers – Functions and Control Flow
#8It'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…
Nested ternary if statements scare me from my PHP days, and noticed inlined closures can at times be harder to grok at a glance.
Re: Rust for JavaScript Developers – Functions and Control Flow
#9Rust 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 traffic to USB device
in node.js turned into battles with dyn FnMut traits to pass "callback/event" handlers around, spawning threads to not block on libusb reading / WebSocket client reading, borrowing, cloning, mutex locking, reference counting with Rust like you can in JavaScript.
You can't just "move variables from one scope into callback/lambda" scope with any sort of ease in Rust.
Re: Rust for JavaScript Developers – Functions and Control Flow
#10I 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…
Rust is “move” by default. Passing ownership of a variable to callbacks requires making sure you have the correct types along the way, especially if you want to mutate. It can be easier in many cases to only deal with “owned” data in Rust at first. Clone a lot, and so on.