3 ms·
I'm glad that I provide amusement, but rustc-1.63.0 still compiles this program that panics at runtime. My experience with Rust is 5 min, so the style may provi
by bgwalter 11mo ago
I'm glad that I provide amusement, but rustc-1.63.0 still compiles this program that panics at runtime. My experience with Rust is 5 min, so the style may provide further amusement:
fn f(n : usize) -> usize {
if n == 0 {
10
} else if n % 32327 == 0 {
1
}
else {
f(n-1)
}
}
fn main() {
let a = [1,2,3,4,5];
let n = f(12312);
println!("{:?}", a[n]);
}
- dwattttt 11mo agoAt compile time, Rust guaranteed memory safety here by ensuring a runtime check was present, which your code triggered, resulting in a panic. A panic is not a violation of memory safety; if you wanted to violate memory safety you'd need to have caused that deref to succeed, and println to spit out the result. EDIT: let me guess, did the panic message include "index out of bounds: the len is 5 but the index is 10"?
- bgwalter 11mo agohttps://news.ycombinator.com/item?id=45882148 https://news.ycombinator.com/item?id=45882148
- mustache_kimono 11mo ago> but rustc-1.63.0 still compiles this program that panics at runtime. An index OOB error? Here, it's important to remember the Rust panic is still memory safe. Perhaps you should read the article, or read up on what undefined behavior is?[0] Here, the Rust behavior is very well defined. It will either abort or unwind.[1] If you prefer different behavior, there is the get method on slices.[2] [0]: https://en.wikipedia.org/wiki/Undefined_behavior https://en.wikipedia.org/wiki/Undefined_behavior [1]: https://doc.rust-lang.org/reference/panic.html https://doc.rust-lang.org/reference/panic.html [2]: https://doc.rust-lang.org/std/primitive.slice.html#method.get https://doc.rust-lang.org/std/primitive.slice.html#method.ge...
- bgwalter 11mo agoThis subthread is about claims of STATIC checking of memory safety. A panic is not static checking. Perhaps you should read what you respond to.
- unrealhoang 11mo agoA panic is memory-safe, so static checking of memory safety holds. Perhaps you should understand your own claims.
- bgwalter 11mo agoI'll henceforth refer to the process of using vector.at(0) instead of vector[0] in C++ as "providing memory safety by static analysis". Static analysis has a specific meaning, and rote insertion of bounds checking isn't it.
- unrealhoang 11mo agoMemory safety has a specific meaning, and panic isn’t it. C++ can have UB, compilable non-unsafe Rust can’t, that’s what static analysis of memory safety is. Main point here is you don’t know (and refuse to learn) new knowledge.
- bgwalter 11mo ago[flagged]
- estebank 11mo agoIf the only way of triggering spatial memory unsafety in C++ was vector[i] and that operation was defined to always interrupt execution, then yes, C++ would be considered memory safe. But that is not the case. The equivalent of vector[i] in Rust is Vex::get_unchecked, which is marked as unsafe, not the default that people reach for normally.