cognitive-engineering-lab / rust-book

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

Ownership chapt can be improved by having problematic examples in C and solutions in Rust #180

Open happy-dude opened 2 months ago

happy-dude commented 2 months ago

URL to the section(s) of the book with this problem: https://rust-book.cs.brown.edu/ch04-02-references-and-borrowing.html

Description of the problem: Hey team, I am really enjoying the brown.edu interactive version of the Rust book. I found the Ownership chapter and sections very informative, especially compared to the verbosity and density in the original.

One thought that my friends and I had while reading this chapter was:

To help alleviate this, I propose that the chapter can be improved by writing the erroneous / undefined behavior bits in C and demonstrating afterwards how Rust is an improvement.

Suggested fix: By demonstrating the issues in C,

block0xhash commented 2 months ago
  1. Best place to practice this is here where you will see what's happening under the hood https://cognitive-engineering-lab.github.io/aquascope/

  2. What about those who never coded in C . Will they need to learn C first as a prerequisite or

  3. does the book need to teach C and pointers before Rust ?

I actually read that chapter over and over until I got it. I guess this is part of the "experiment". If I didn't get the answer right I assumed I didn't understand and went back and reread.

The metal block you mention I unblocked by actually reading and "dry running" the code and not using the C complier. This helps to burn the understanding in your brain

happy-dude commented 2 months ago

1) aquascope is great; the chapter is already using it to help establish the rules of Rust, but still doesn't address the concerns I raised in the issue description:

2) no, C is not a prerequisite 3) no, because pointers and references are already being introduced in the Ownership chapter

My preference for using C for the bad-practice examples is because:

You do not have to learn C as a prerequisite to successfully accomplish this.

The metal block you mention I unblocked by actually reading and "dry running" the code and not using the C complier. This helps to burn the understanding in your brain

Re-reading a section multiple times implies that the section was not clear. I argue that instead of gaining a deeper foundational understanding of why the concept is important or necessary, they are instead falling back onto rote memorization of what the language is trying to do.

As an analogy: the reader is learning to wear a helmet when riding a bicycle, but not why the helmet is helpful in the first place.

willcrichton commented 1 week ago

Hi @happy-dude, the short answer is: because we believe it makes you a better Rust programmer. We have done a fair amount of science to justify that claim, described in this paper: https://dl.acm.org/doi/10.1145/3622841

The slightly longer answer: "rust doesn't let you make these mistakes" isn't quite right. A programmer can still make the mistake of writing unsafe (in the sense of violation the borrow checker, not in the sense of using the unsafe keyword) code. It's just that Rust catches this mistake at compile-time rather than run-time. So when you say "we would never write code like these examples", that isn't true! People write code like these examples all the time. When Rust says "this is unsafe", it's then up to the programmer to understand: why is this unsafe? How should I best fix it?

In particular, I added Section 4.3 "Fixing Ownership Errors" to show how thinking in terms of memory safety helps you understand how to fix borrow checker errors. That's the key idea: the deeper understanding helps you know how to respond to the compiler's feedback.