Live data from Hacker News

Thoughts on Rust, a few thousand lines in

rcoh.me

11–20 of 183 posts

Re: Thoughts on Rust, a few thousand lines in

#11

Earlier quoted context omitted.

Swift does, but typically for Optional promotion. You see stuff like `guard let delegate = delegate else { return }` inside functions all the time, shadowing a property with a local & promoting the type from Optional to T. It's not the same as the Rust example because you're shadowing an ivar to a local, but since `self.` is implicit you're still shadowing.

> for Optional promotion You'll see the same in Rust fn example(name: Option ) -> Option { let name = name?; Some(name.len()) } Or fn example(name: Option ) { if let Some(name) = name { println!("{}", name.len()); } } A main difference is the requirement to use `Some`, which allows for the flexibility to apply to any enum. > but since `self.` is implicit To make sure I'm following, do you mean that Rust's `self.` is…

That all makes sense. I've only done a couple days of Rust, so my recollection is spotty. The shadowing makes sense because if a local variable was moved out, then you're not really shadowing it anymore. Is my understanding of that correct?

> do you mean that Rust's `self.` is implicit in Swift?

Swift's `self.` is implicit in Swift – in most contexts, to access a property the `self.` is not required. `self.name = "John"` and `name = "John"` are equivalent (assuming self is an object with a name property).

There are places where explicit `self.` is required though – when you want to differentiate between a shadowed local and a property (obviously), or when you're inside a closure (to make it clear that the closure is capturing self, not just capturing a reference to the property).

Re: Thoughts on Rust, a few thousand lines in

#12

I've not done any rust dev before but the whole variable shadowing thing really scares me, in that it makes it not obvious where the initial declaration comes from. This seems difficult to reason about.

Two things, 1) the compiler doesn’t really allow you to screw it up in really scary ways, but you can I suppose. 2) it allows you to rebind a variable name to a new type, of a new state during execution without coming up with a hokey name for the new thing that’s really just the original modified in some way.

Re: Thoughts on Rust, a few thousand lines in

#13

let foo = "..."; let foo = parse(foo); let foo = escaped(foo); Is it really shadowing or mutation of foo? I would consider this shadowing (something you can do in OcaML): let foo = "..." in let foo = parse(foo) in let foo = escaped(foo) in dosomething(foo);;

You can't mutate variables in rust without declaring mut

Re: Thoughts on Rust, a few thousand lines in

#14

let foo = "..."; let foo = parse(foo); let foo = escaped(foo); Is it really shadowing or mutation of foo? I would consider this shadowing (something you can do in OcaML): let foo = "..." in let foo = parse(foo) in let foo = escaped(foo) in dosomething(foo);;

It is shadowing (though it could be converted into mutation by a sufficiently smart compiler...). ReasonML does something similar with their take on Ocaml.

https://reasonml.github.io/docs/en/let-binding

Re: Thoughts on Rust, a few thousand lines in

#15
post #13

let foo = "..."; let foo = parse(foo); let foo = escaped(foo); Is it really shadowing or mutation of foo? I would consider this shadowing (something you can do in OcaML): let foo = "..." in let foo = parse(foo) in let foo = escaped(foo) in dosomething(foo);;

You can't mutate variables in rust without declaring mut

But how do you know just by reading that code? It would not be obvious to me at all.

Re: Thoughts on Rust, a few thousand lines in

#16

> the escape hatch suggested on the internet, `partial_cmp(...).unwrap_or(Ordering::Less)` This is often a Bad Idea, as you get unstable sorts and you are right back to the same problem. - Explanation: How do I get the minimum or maximum value of an iterator containing floating point numbers? — https://stackoverflow.com/a/50308360/155423 - Example: https://play.integer32.com/?version=stable&mode=debug&editio... See a…

Isn't there a default total order for floats? E.g. -Inf < -1.0 < -0.0 < 0.0 < 1.0 < Inf < NaN?

Re: Thoughts on Rust, a few thousand lines in

#17
> A lot of great, well loved, crates don’t have a lot of Github stars.

This was a really important learning for me. When I'm looking for crates to solve a problem and there's only a handful of them, I almost always go through every single one of them, even if some have thousands and the others only tens of downloads.

It's an incredible feeling to find those unknown diamonds..

Re: Thoughts on Rust, a few thousand lines in

#20

> the escape hatch suggested on the internet, `partial_cmp(...).unwrap_or(Ordering::Less)` This is often a Bad Idea, as you get unstable sorts and you are right back to the same problem. - Explanation: How do I get the minimum or maximum value of an iterator containing floating point numbers? — https://stackoverflow.com/a/50308360/155423 - Example: https://play.integer32.com/?version=stable&mode=debug&editio... See a…

Isn't there a default total order for floats? E.g. -Inf < -1.0 < -0.0 < 0.0 < 1.0 < Inf < NaN?

No, because x NaN are false for any value of x.
Post reply on HN