From 4fcb5d41bd72739b76beac0466a2dd59e403138b Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Fri, 7 Aug 2015 16:05:48 +0200 Subject: [PATCH 1/1] feedback from Alexis. Thanks! --- solutions/src/list.rs | 2 +- src/main.rs | 22 ++++++++-------------- src/part16.rs | 6 +++--- workspace/src/part16.rs | 6 +++--- 4 files changed, 15 insertions(+), 21 deletions(-) diff --git a/solutions/src/list.rs b/solutions/src/list.rs index 5137555..3206982 100644 --- a/solutions/src/list.rs +++ b/solutions/src/list.rs @@ -101,7 +101,7 @@ impl LinkedList { } } - pub fn iter_mut(&self) -> IterMut { + pub fn iter_mut(&mut self) -> IterMut { IterMut { next: self.first, _marker: PhantomData } } } diff --git a/src/main.rs b/src/main.rs index b08b1da..942e490 100644 --- a/src/main.rs +++ b/src/main.rs @@ -23,12 +23,12 @@ // achieve C++-style control over memory and execution behavior (like, static vs. dynamic // dispatch), which makes it possible to construct abstractions that carry no run-time // cost. This is combined with the comfort of high-level functional languages and guaranteed -// safety (as in, the program will not crash). The vast majority of existing -// languages sacrifices one of these goals for the other. In particular, the -// first requirement rules out a garbage collector: Rust can run "bare metal". -// In fact, Rust rules out more classes of bugs than languages that achieve safety -// with a GC: Besides dangling pointers and double-free, Rust also prevents issues -// such as iterator invalidation and data races. +// safety (as in, the program will not crash in uncontrolled ways). The vast majority of existing +// languages sacrifices control for safety (for example, by enforcing the usage of +// a garbage collector) or vice versa. Rust can run without dynamic allocation (i.e., without +// a heap), and even without an operating system. In fact, Rust rules out more classes of bugs +// than languages that achieve safety with a garbage collector: Besides dangling pointers and +// double-free, Rust also prevents issues such as iterator invalidation and data races. // // // Getting started @@ -39,20 +39,14 @@ // (at the time of writing, that's the current beta release). More detailed installation // instructions are provided in [the second chapter of The Book](https://doc.rust-lang.org/stable/book/installing-rust.html). // This will also install `cargo`, the tool responsible for building rust projects (or *crates*). - +// // Next, fetch the Rust-101 source code from the [git repository](http://www.ralfj.de/git/rust-101.git) // (also available [on GitHub](https://github.com/RalfJung/rust-101), and as a // [zip archive](https://github.com/RalfJung/rust-101/archive/master.zip) in case you don't have git installed). // // There is a workspace prepared for you in the `workspace` folder. I suggest you copy this -// folder somewhere else - that will make it much easier to later update the course without -// overwriting your changes. Try `cargo build` in that new folder to check that compiling your workspace succeeds. +// folder somewhere else. Try `cargo build` in that new folder to check that compiling your workspace succeeds. // (You can also execute it with `cargo run`, but you'll need to do some work before this will succeed.) -// -// If you later want to update the course, do `git pull` (or re-download the zip archive). -// Then copy the files from `workspace/src/` to your workspace that you did not yet work on. Definitely -// copy `main.rs` to make sure all the new files are actually compiled. (Of course you can also -// copy the rest, but that would replace all your hard work by the original files with all the holes!) // Course Content // -------------- diff --git a/src/part16.rs b/src/part16.rs index 3b0a1d0..dc26369 100644 --- a/src/part16.rs +++ b/src/part16.rs @@ -84,7 +84,7 @@ impl LinkedList { //@ Calling `box_into_raw` gives up ownership of the box, which is crucial: We don't want the memory that it points to to be deallocated! let new = Box::new( Node { data: t, next: ptr::null_mut(), prev: self.last } ); let new = box_into_raw(new); - // Update other points to this node. + // Update other pointers to this node. if self.last.is_null() { debug_assert!(self.first.is_null()); // The list is currently empty, so we have to update the head pointer. @@ -106,7 +106,7 @@ impl LinkedList { // Next, we are going to provide an iterator. //@ This function just creates an instance of `IterMut`, the iterator type which does the actual work. - pub fn iter_mut(&self) -> IterMut { + pub fn iter_mut(&mut self) -> IterMut { IterMut { next: self.first, _marker: PhantomData } } } @@ -173,7 +173,7 @@ impl<'a, T> Iterator for IterMut<'a, T> { //@ of `LinkedList`. impl Drop for LinkedList { // The destructor itself is a method which takes `self` in mutably borrowed form. It cannot own `self`, because then - // the destructor of `self` would be called at the end pf the function, resulting in endless recursion... + // the destructor of `self` would be called at the end of the function, resulting in endless recursion... fn drop(&mut self) { let mut cur_ptr = self.first; while !cur_ptr.is_null() { diff --git a/workspace/src/part16.rs b/workspace/src/part16.rs index 835b8b8..21cd60f 100644 --- a/workspace/src/part16.rs +++ b/workspace/src/part16.rs @@ -42,7 +42,7 @@ impl LinkedList { // Create the new node, and make it a raw pointer. let new = Box::new( Node { data: t, next: ptr::null_mut(), prev: self.last } ); let new = box_into_raw(new); - // Update other points to this node. + // Update other pointers to this node. if self.last.is_null() { debug_assert!(self.first.is_null()); // The list is currently empty, so we have to update the head pointer. @@ -61,7 +61,7 @@ impl LinkedList { // and return `Option`. // Next, we are going to provide an iterator. - pub fn iter_mut(&self) -> IterMut { + pub fn iter_mut(&mut self) -> IterMut { IterMut { next: self.first, _marker: PhantomData } } } @@ -97,7 +97,7 @@ impl<'a, T> Iterator for IterMut<'a, T> { impl Drop for LinkedList { // The destructor itself is a method which takes `self` in mutably borrowed form. It cannot own `self`, because then - // the destructor of `self` would be called at the end pf the function, resulting in endless recursion... + // the destructor of `self` would be called at the end of the function, resulting in endless recursion... fn drop(&mut self) { let mut cur_ptr = self.first; while !cur_ptr.is_null() { -- 2.30.2