豆瓣
扫码直接下载
读过 Programming Rust, 2nd Edition
A panic is not a crash. It’s not undefined behavior. It’s more like a RuntimeException in Java or a std::logic_error in C++. The behavior is well-defined; it just shouldn’t be happening. Panic is safe. It doesn’t violate any of Rust’s safety rules; even if you manage to panic in the middle of a standard library method, it will never leave a dangling pointer or a half-initialized value in memory. The idea is that Rust catches the invalid array access, or whatever it is, before anything bad happens. It would be unsafe to proceed, so Rust unwinds the stack. But the rest of the process can continue running. Panic is per thread. One thread can be panicking while other threads are going on about their normal business. In Chapter19, we’ll show how a parent thread can find out when a child thread panics and handle the error gracefully. There is also a way to catch stack unwinding, allowing the thread to survive and continue running. The standard library function std::panic::catch_unwind() does this. We won’t cover how to use it, but this is the mechanism used by Rust’s test harness to recover when an assertion fails in a test. (It can also be necessary when writing Rust code that can be called from C or C++, because unwinding across non-Rust code is undefined behavior; see Chapter22.)引自第1页Stack unwinding is the default panic behavior, but there are two circumstances in which Rust does not try to unwind the stack. If a .drop() method triggers a second panic while Rust is still trying to clean up after the first, this is considered fatal. Rust stops unwinding and aborts the whole process. Also, Rust’s panic behavior is customizable. If you compile with -C panic=abort, the first panic in your program immediately aborts the process. (With this option, Rust does not need to know how to unwind the stack, so this can reduce the size of your compiled code.)引自第1页
> 夏夜寂寞属壁虎的所有笔记(179篇)
全篇重点,务必深入理解内存模型。配合 https://cheats.rs/ 的 Data Layout 看更有用。 P.S...
表示其中内容是对原文的摘抄