Live data from Hacker News

Thoughts on Rust, a few thousand lines in

rcoh.me

51–60 of 183 posts

Re: Thoughts on Rust, a few thousand lines in

#51

> Unlike nearly every language I’ve ever used, Rust actually encourages variable shadowing. Haskell does it too, for the same reason/purpose / with the same effect.

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.

I actually really try to avoid this pattern. For one it pollutes the function scope with a shadowed binding and it's pretty unnecessary boilerplate. Most always you can map across the optional and use the unwrapped version as the block scoped argument identifier, e.g. `$0`. When you can't or you want to handle both cases, I find a simple `if let delegate = delegate {} else {}` is more explicit and scopes the binding to the block instead of polluting the current scope. I'm not saying `guard` is bad, I use it all the time when there are good names to bind things to, but I dislike how much it proliferates and how often it doesn't actually provide value and just lets the program silently fail instead of loudly fail which half the time is worse than a nullpointerexception anyway...

Re: Thoughts on Rust, a few thousand lines in

#52

> 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…

OP here -- this post is pretty dated. I currently use the OrderedFloat [1] crate to solve this problem which works quite well and plays nicely with NaN

https://crates.io/crates/ordered-float

Re: Thoughts on Rust, a few thousand lines in

#53
post #43

Earlier quoted context omitted.

Why not this? fn main() { let a = "ab早".as_bytes(); let a = &a[..3]; println!("Hello, world!"); }

It's not clear to me what you're suggesting; is it that String shouldn't have supported indexing in the first place? That code does work, but you have a &[u8] not a &str.

[deleted]

Re: Thoughts on Rust, a few thousand lines in

#54

One thing I don't like about Rust is how taking a slice of a string can cause a runtime panic if the start or end of the slice ends up intersecting a multi-byte UTF-8 char. I would prefer it if this feature didn't exist at all rather than cause runtime panics. https://play.rust-lang.org/?gist=e02ce5e9aacfee3a2b4917d5624...

Go indexes bytes on strings, even though there's a builtin type called Rune which delimits utf-8 codepoints. This is yet another footgun. Is there a language that doesn't handle this poorly?

https://play.golang.org/p/CkBp0w8T621

Re: Thoughts on Rust, a few thousand lines in

#55

> Like Go, Rust can compile statically linked linux binaries. The GNU C library (needed not only by C programs for C things) doesn't support static linking, so the only way this is possible is to use another library entirely, or raw inlined syscalls (where applicable).

Yes, we have full support for MUSL. It's a $ rustup target add x86_64-unknown-linux-musl $ cargo build --target x86_64-unknown-linux-musl away.

Yeah, Rust's musl support is great. We use it heavily at work for all sorts of CLI tools. Many thanks to everybody who worked on this.

(And if you need to link against common C libraries like OpenSSL or PostgreSQL, I maintain a Docker image with the necessary C toolchains, and instructions on how to use it: https://github.com/emk/rust-musl-builder. There are a couple of similar images out there, too, I think.)

Re: Thoughts on Rust, a few thousand lines in

#56
post #43

Earlier quoted context omitted.

Why not this? fn main() { let a = "ab早".as_bytes(); let a = &a[..3]; println!("Hello, world!"); }

It's not clear to me what you're suggesting; is it that String shouldn't have supported indexing in the first place? That code does work, but you have a &[u8] not a &str.

I think he's suggesting that slicing on strings should be by character, and if you want to slice on bytes, you should explicitly ask to treat the string as a byte array. It makes more sense semantically, and it's safe.

Re: Thoughts on Rust, a few thousand lines in

#57
post #50

Rust is one of the few times where the language/ecosystem does precisely what I wished it would do. For instance, being able to partially destructure JSON into a struct in a typesafe manner is just awesome.

This sounds really useful, you don't happen to have an example of this do you? Thanks in advance!

Hi -- OP here. I actually do it in the tests (but for TOML): https://github.com/rcoh/angle-grinder/blob/master/tests/inte...

https://github.com/rcoh/angle-grinder/blob/master/tests/stru...

Re: Thoughts on Rust, a few thousand lines in

#58
post #18

Earlier quoted context omitted.

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

You mean if you don't know the language?

I guess I just need to learn Rust. I wouldn't write a style of code where I have 3 lets after an other and using the same variable name.

Re: Thoughts on Rust, a few thousand lines in

#59

Earlier quoted context omitted.

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

let foo = bar; let foo = bat; is shadowing let foo = bar; foo = bat; is a compilation-time error because foo isn't mutable. let mut foo = bar; foo = bat; is reassignment. in working code, either foo is declared as mutable or it's not, and it's pretty obvious from the code what's happening.

I guess. I would just never write code like that.
Post reply on HN