logoalt Hacker News

randyrand • today at 1:08 PM • 3 replies • view on HN

I just wish Rust handled out of memory more gracefully like a normal error.


Replies

bluGill • today at 1:19 PM

Why bother? Most systems these days have over commit and so you can never get an out of memory error. The OS will happily give you more and more RAM, and then when you try to use that memory kill a program (may or not be your program). The only out of memory error you can see is if you are trying to allocate more memory than the address space allows. This is so unlikely that nobody considers it worth bother to try to handle.

➕ show 1 reply
yencabulator • today at 10:04 PM

On a typical Linux box, overcommit and eager or confused OOM killer will ruin your day anyway.

For no_std style work where running out of memory is actually a hard limit, you have two choices:

1. disable the global allocator and use APIs that take Allocator, to force yourself to only consume APIs that can return errors from allocation. (The ergonomics aren't always great, and fattening all types with a pointer to Allocator isn't great; might be able to use a zero-sized Allocator, with trickery. Say goodbye to many libraries.)

2. use static analysis and try to avoid ever hitting a code path like Vec::with_capacity that doesn't return a Result. Make sure the tooling also analyses your dependencies. (Tooling for the static analysis is painful. And still, say goodbye to many libraries.)

I do wish there were more conveniences for alloc-can-fail APIs, like the failable version of Vec::push needs both try_reserve and push_within_capacity (nightly only).

efficax • today at 2:54 PM

the biggest design wart of rust (well one of them) is that support for custom allocators wasn't built into the type system from the start, with allocation osmething that returns Result

➕ show 1 reply