jzarnett / ece459

ECE 459: Programming for Performance
436 stars 136 forks source link

Rust lecture is a bit out of date #43

Closed sunjay closed 4 years ago

sunjay commented 4 years ago

Hello! I'm enjoying the course so far. I was curious about what you would talk about when it came to Rust, so I looked a bit ahead. :)

Slide 29/38 in Lecture 35: Rust is out of date. It shows this example and says that it doesn't compile:

let mut s = String::from("one");
let r1 = &mut s;
let r2 = &mut s;

However, if you run this today, it will compile with no issues.

The reason for this is because about a year ago Rust added a feature called Non-Lexical Lifetimes (NLL). That makes it so that the lifetime of a reference is no longer only tied to the length of its scope. The lifetime is now tied to how long it is used (up to the end of its scope of course).

To make this code stop compiling, you have to mutate r1. This extends its lifetime to past the declaration of r2, which means that creating the mutable reference is r2 is now illegal.

let mut s = String::from("one");
let r1 = &mut s;
let r2 = &mut s;
// This line causes the code to stop compiling
r1.push_str(" two");

Some of the other slides also refer to references having to go out of scope in order for you to be able to create new &mut references. This is no longer 100% true because of NLL.

Some resources on NLL if you're interested:

patricklam commented 4 years ago

Neat! I'll try to update the written lecture notes (I guess there's a lot of time) and I'll leave the slides to Jeff.

pat

On Tue, Jan 21, 2020, 7:18 AM Sunjay Varma notifications@github.com wrote:

Hello! I'm enjoying the course so far. I was curious about what you would talk about when it came to Rust, so I looked a bit ahead. :)

Slide 29/38 in Lecture 35: Rust is out of date. It shows this example and says that it doesn't compile:

let mut s = String::from("one");let r1 = &mut s;let r2 = &mut s;

However, if you run this today https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=641a7e5d4fdd5a4f3a93ab67ee910bdb, it will compile with no issues.

The reason for this is because about a year ago Rust added a feature called Non-Lexical Lifetimes (NLL). That makes it so that the lifetime of a reference is no longer only tied to the length of its scope. The lifetime is now tied to how long it is used (up to the end of its scope of course).

To make this code stop compiling https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=04afe4d9f7b157018da59ce821feda99, you have to mutate r1. This extends its lifetime to past the declaration of r2, which means that creating the mutable reference is r2 is now illegal.

let mut s = String::from("one");let r1 = &mut s;let r2 = &mut s;// This line causes the code to stop compiling r1.push_str(" two");

Some of the other slides also refer to references having to go out of scope in order for you to be able to create new &mut references. This is no longer 100% true because of NLL.

Some resources on NLL if you're interested:

- https://doc.rust-lang.org/edition-guide/rust-2018/ownership-and-lifetimes/non-lexical-lifetimes.html

http://smallcultfollowing.com/babysteps/blog/2016/04/27/non-lexical-lifetimes-introduction/

http://smallcultfollowing.com/babysteps/blog/2016/05/04/non-lexical-lifetimes-based-on-liveness/

http://smallcultfollowing.com/babysteps/blog/2016/05/09/non-lexical-lifetimes-adding-the-outlives-relation/

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/jzarnett/ece459/issues/43?email_source=notifications&email_token=AAOKE5UU6FQ5SEBJM46PWP3Q6XTIPA5CNFSM4KJIGJSKYY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4IHNWMTA, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAOKE5Q4BTWCMGMUHW53ZMLQ6XTIPANCNFSM4KJIGJSA .

patricklam commented 4 years ago

Whoops! I was working from memory and emailed the wrong s*varma. This time I'm at a computer and I double checked.

I've pushed the following diffs to the repo. Sunjay, could you double check to see that I wrote things that are correct? Thanks!

pat

On Tue, Jan 21, 2020 at 10:39 AM Patrick Lam prof.lam@gmail.com wrote:

Neat! I'll try to update the written lecture notes (I guess there's a lot of time) and I'll leave the slides to Jeff.

pat

On Tue, Jan 21, 2020, 7:18 AM Sunjay Varma notifications@github.com wrote:

Hello! I'm enjoying the course so far. I was curious about what you would talk about when it came to Rust, so I looked a bit ahead. :)

Slide 29/38 in Lecture 35: Rust is out of date. It shows this example and says that it doesn't compile:

let mut s = String::from("one");let r1 = &mut s;let r2 = &mut s;

However, if you run this today https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=641a7e5d4fdd5a4f3a93ab67ee910bdb, it will compile with no issues.

The reason for this is because about a year ago Rust added a feature called Non-Lexical Lifetimes (NLL). That makes it so that the lifetime of a reference is no longer only tied to the length of its scope. The lifetime is now tied to how long it is used (up to the end of its scope of course).

To make this code stop compiling https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=04afe4d9f7b157018da59ce821feda99, you have to mutate r1. This extends its lifetime to past the declaration of r2, which means that creating the mutable reference is r2 is now illegal.

let mut s = String::from("one");let r1 = &mut s;let r2 = &mut s;// This line causes the code to stop compiling r1.push_str(" two");

Some of the other slides also refer to references having to go out of scope in order for you to be able to create new &mut references. This is no longer 100% true because of NLL.

Some resources on NLL if you're interested:

- https://doc.rust-lang.org/edition-guide/rust-2018/ownership-and-lifetimes/non-lexical-lifetimes.html

http://smallcultfollowing.com/babysteps/blog/2016/04/27/non-lexical-lifetimes-introduction/

http://smallcultfollowing.com/babysteps/blog/2016/05/04/non-lexical-lifetimes-based-on-liveness/

http://smallcultfollowing.com/babysteps/blog/2016/05/09/non-lexical-lifetimes-adding-the-outlives-relation/

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/jzarnett/ece459/issues/43?email_source=notifications&email_token=AAOKE5UU6FQ5SEBJM46PWP3Q6XTIPA5CNFSM4KJIGJSKYY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4IHNWMTA, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAOKE5Q4BTWCMGMUHW53ZMLQ6XTIPANCNFSM4KJIGJSA .

sunjay commented 4 years ago

Looks great! Thank you for taking the time to update the notes. :)

It looks like the tex file has been updated, but the PDF still needs to be regenerated.

jzarnett commented 4 years ago

Over to me for the slides.

sunjay commented 4 years ago

Also relevant to this course, Rust recently added support for Profile Guided Optimizations: https://doc.rust-lang.org/rustc/profile-guided-optimization.html