cognitive-engineering-lab / rust-book

The Rust Programming Language: Experimental Edition
https://rust-book.cs.brown.edu
Other
503 stars 82 forks source link

Ch4 ownership needs better examples. #183

Open CybCom opened 2 months ago

CybCom commented 2 months ago

I was reading here and I don't think the code here would be a good example for demonstrating the safety of rust.

fn main() {
    read(x); // oh no! x isn't defined!
    let x = true;
}

Yes, this code is not safe if allowed to run. However, use a variable without declaration will cause an compiling error in most languages (java, c...) rather than only rust. And its also not complex to check.

Maybe this,

fn main() {
    let x;
    read(x); // oh no! x isn't initialized!
}

use a variable uninitialized will be a better example to explain the concept of undefined behavior.

willcrichton commented 6 days ago

The example is intentionally designed to show behavior that would be unsafe in any language, since it's at the very beginning of the section.

CybCom commented 6 days ago

Under this example the book states the advantage of rust over interpreted langs, and I just want to also show its advantage over other compiled langs by editing the example😂