Live data from Hacker News

Thoughts on Rust, a few thousand lines in

rcoh.me

121–130 of 183 posts

Re: Thoughts on Rust, a few thousand lines in

#121
post #89

One thing Rust doesn't seem to be doing very well yet is guard clauses, specifically when handling Option . I've seen and appreciated the use of guard clauses in many languages, as a good way to quickly check for a few conditions at the top of a function, and return early if those conditions aren't met. Since it seems that Option are recommended in Rust, there's a lot of time you want to quickly return if `Some(x)` i…

You can use ? on an Option if your type returns an Option. If it returns a Result, you can use ok_or()?, and at some point in the nearish future, you can just use ?.

I see, not very familiar with both those idioms `?` and `ok_or()?`

My current understanding is that those would return an Error only? I was more describing cases where you do want to return, but not necessarily return an `Error`.

For instance in a simplified example function that returns a boolean, you could decide to return `false`. is it possible there?

    // Function that returns a boolean value
    fn is_equal_to_ten(n: Option) -> bool {
        // some one liner  that checks for None, if it's not none, gives you `x` when `n` matches content of `Some(x)` (not real code): 
        if let Some(x) = n else { return false; /* what to do in case it's a None*/ } 
        
        // `x` is available here:
        return (x == 10);        
    }

Would this be considered bad practice in Rust?

Re: Thoughts on Rust, a few thousand lines in

#122
post #97

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

This seems specious to me. The only way to get an invalid index in a string in any language is that you either have an array index arithmetic error or you are blindly operating on a string you haven't validated . If you want all the data after a : character, you slice on the index of the :. The character after it is going to be the beginning of a UTF-8 character. You do not under any circumstances guess that the colo…

I just realized that I have bug in my GPS driver. It operates on ASCII data, so [] operator is safe, BUT data can be corrupted (low chance, but non-zero), so it can form valid multibyte character, so my code will panic on it, trying to parse and validate NMEA message.

Re: Thoughts on Rust, a few thousand lines in

#123
post #108

Earlier quoted context omitted.

I’m basically talking about Serde. Read the section titled “Parsing JSON as strongly typed data structures”. What’s nice is that you don’t have to include all the fields in the JSON in the struct. Serde will only give you the ones defined by the struct. https://github.com/serde-rs/json/blob/master/README.md

I'm not sure I'd consider that lib any safer than the average serialization library when things like this happen: https://github.com/serde-rs/json/issues/464

Panics are memory safe, so that’s still more safe than many parsing bugs.

Re: Thoughts on Rust, a few thousand lines in

#124
post #121

Earlier quoted context omitted.

You can use ? on an Option if your type returns an Option. If it returns a Result, you can use ok_or()?, and at some point in the nearish future, you can just use ?.

I see, not very familiar with both those idioms `?` and `ok_or()?` My current understanding is that those would return an Error only? I was more describing cases where you do want to return, but not necessarily return an `Error`. For instance in a simplified example function that returns a boolean, you could decide to return `false`. is it possible there? // Function that returns a boolean value fn is_equal_to_ten(n:…

The question mark operator works via a trait, Try. Both Option and Result implement Try. If you use ? on a None value, it will return None, just like using ? on an Err returns an Err.

ok_or is a method on Option that would let you manually convert it to a Result. You could then combine it with ?, turning a None into a specific Err.

It won’t help for stuff that returns bool, it’s true.

Re: Thoughts on Rust, a few thousand lines in

#125
post #121

Earlier quoted context omitted.

I see, not very familiar with both those idioms `?` and `ok_or()?` My current understanding is that those would return an Error only? I was more describing cases where you do want to return, but not necessarily return an `Error`. For instance in a simplified example function that returns a boolean, you could decide to return `false`. is it possible there? // Function that returns a boolean value fn is_equal_to_ten(n:…

The question mark operator works via a trait, Try. Both Option and Result implement Try. If you use ? on a None value, it will return None, just like using ? on an Err returns an Err. ok_or is a method on Option that would let you manually convert it to a Result. You could then combine it with ?, turning a None into a specific Err. It won’t help for stuff that returns bool, it’s true.

Thanks for the detailed answer :)

Re: Thoughts on Rust, a few thousand lines in

#126
post #87

Earlier quoted context omitted.

Panics are not unsafe. Panic exists in Rust because they are safe. If you don't want a panic on index, just don't index. Indexing into a UTF-8 string doesn't serve any reasonable consistent purpose anyway, because it is an abstraction of text that doesn't provide support to the notion that a "character" is more fundamental than a word or paragraph, etc. Rust's string slicing exists solely to make ASCII text easy to h…

I think that's too extreme. There are many legitimate reasons to slice non-ASCII text - for example, to split it on newlines.

That's not trivial and different languages vary in how they handle new line characters even. https://stackoverflow.com/questions/44995851/how-do-i-check-...

Re: Thoughts on Rust, a few thousand lines in

#127
post #108

Earlier quoted context omitted.

I'm not sure I'd consider that lib any safer than the average serialization library when things like this happen: https://github.com/serde-rs/json/issues/464

Panics are memory safe, so that’s still more safe than many parsing bugs.

The issue does not mention memory safety and neither did I. Honestly, knee-jerk reactions like "BUT MUH MEMORY SAFETY" doesn't inspire confidence specially when it couldn't help saving that dev from the troubles and bugs documented in the issue. To quote a few:

> it was a hassle to track down because Rust itself didn't complain and the panic message during serialization wouldn't tell me which file of the hundreds of thousands was causing it to die. For lack of a purpose-built tool, I had to manually bisect it until I narrowed it down.

> That said, definitely a footgun in the standard library to be remedied.

> My main concern here is getting rid of the footgun if at all possible. I really don't want to have to maintain a special "Never allow these types to creep into structs I'm deriving Serialize/Deserialize on, because the compiler certainly won't warn you" audit list.

If that's considered safe in Rust's standards then I rest my case.

Re: Thoughts on Rust, a few thousand lines in

#128

Earlier quoted context omitted.

The obvious follow up question would be: so why is slicing a string a byte-wise operation and not a character-wise operation? If a string is an array of characters, why does it let me refer to individual bytes without explicitly casting it to a byte array? How often comparatively do you want the nth byte compared to the nth character? I would suspect that's pretty rare.

As stated below, indexing is an O(1) operation, and that is a O(n) operation. > If a string is an array of characters It is not, it is an array (technically vector) of bytes.

Who cares if it's O(1) if it causes a panic? What good is high performance if it doesn't complete or isn't safe?

At the very least, shouldn't there be an O(n) method to do character-wise slicing?

Re: Thoughts on Rust, a few thousand lines in

#129

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

You can also download the cross-compilation tarball for musl at https://static.rust-lang.org/dist/rust-std-1.31.0-x86_64-unk... if you installed rust that way. You'd then build normally:

    $ cargo build --target x86_64-unknown-linux-musl

Re: Thoughts on Rust, a few thousand lines in

#130
post #127

Earlier quoted context omitted.

Panics are memory safe, so that’s still more safe than many parsing bugs.

The issue does not mention memory safety and neither did I. Honestly, knee-jerk reactions like "BUT MUH MEMORY SAFETY" doesn't inspire confidence specially when it couldn't help saving that dev from the troubles and bugs documented in the issue. To quote a few: > it was a hassle to track down because Rust itself didn't complain and the panic message during serialization wouldn't tell me which file of the hundreds of…

Memory safety issues plague parsers, and often have dire consequences. rust claims to be memory safe. This bug does not invoke memory unsafety.

Yes, things can still be improved, but this is nowhere near as bad as many parsing bugs.

Post reply on HN