Hacker News new | ask | show | jobs
by Chai-T-Rex 19 days ago
That's not really a memory safety bug, due to the fact that it panics rather than doing something unsafe.

The `variable[i]` notation is intended to function like in other languages, where it returns an unwrapped value. It would be ugly, noisy, unergonomic, and annoying to have to properly handle `Some` (like in `variable[i].unwrap()`) every single time you ever used it. Imagine doing `c[i].unwrap()[j].unwrap() += a[i].unwrap()[k].unwrap() * b[k].unwrap()[j].unwrap();`.

If you really want the result to be wrapped in `Some`, you can use `variable.get(i)`.

2 comments

> That's not really a memory safety bug, due to the fact that it panics rather than doing something unsafe.

The ? operator can be nicer to read then all those .unwraps() as an alternative and doesn't panic.

I guess the concern with panics is you're not forced to think about how you want to handle them like you do when you get a `Result` or `Option` returned.

As a result it could be used as a DDOS against your app if it's network accessible.

Applying the ? operator to an Option<_> doesn't work inside a function returning io::Result<_>, for example, and turning the None from an out-of-bounds vector access into some io::Result::Err usually wouldn't make sense.

I would go so far as to say that an out-of-bounds index is always a logic error (to be handled by panicking), not an input error (to be handled with Result). And if the out-of-bounds access was caused by invalid input, then logic error consists of not checking the input before indexing.

The former issue arises from post-hoc thinking. Of course it's hard to retrofit into the language, but I see no reason why there shouldn't be facilities for the ? operator to turn a None into an appropriate error type.

The latter is a subjective matter. There are reasons to never want a panic (hello there, Linux kernel) and reasons to do more sophisticated stuff (core dump, cleanup or notifications) before crashing. And maybe one thread shouldn't lead to process death. I think most of these decisions should be kept to the user's discretion.

> no reason why there shouldn't be facilities for the ? operator to turn a None into an appropriate error type

There are facilities for this.

   option.ok_or(MyError::new())?
> do more sophisticated stuff (core dump, cleanup or notifications)

That's what panic::set_hook is for. [1]

> And maybe one thread shouldn't lead to process death.

That's what panic::catch_unwind is for. [2]

[1] https://doc.rust-lang.org/std/panic/fn.set_hook.html

[2] https://doc.rust-lang.org/std/panic/fn.catch_unwind.html

gotta remember tho that ? didn’t exist in 1.0, and now we can’t break existing code
Is that Rust's guarantee? I know that Go avoids breaking changes but I thought they were normal in Rust.
Rust does VERY limited breaking changes, the largest one probably being the `!` type inference change that was very carefully vetted, deemed a necessity and saw contributions to fix the issue in all crates in the official registry
They do break backwards compatibility at edition boundaries, but if they do that too much without a good enough reason they could end up with a python2->python3 situation, with low uptake.
Rust editions exist for handling breaking changes. I dont get why so many people treat them as unthinkable/impossible.
Editions don't version the std lib, so breaking changes to library types aren't generally allowed.
While true, deprecating modules is still possible, if barely, as can be seen by RFC 3550 deprecating legacy ranges in I believe Edition 2027[1]

[1]: https://rust-lang.github.io/rfcs/3550-new-range.html

I'd hope eventually they'll move and we don't end up with the JS situation [1]

[1]: https://news.ycombinator.com/item?id=45447467

GP didn't say it was a memory safety bug.