Menu
Sign In Search Podcasts Libraries Charts People & Topics Add Podcast API Blog Pricing

Alice Ryhl

๐Ÿ‘ค Speaker
505 total appearances

Appearances Over Time

Podcast Appearances

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

And a reference is basically, all it is, is that it's a pointer to the object,

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

And that's it.

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

So we do no checking at runtime.

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

So, of course, this means that compile time, we have to make sure that the last use of the reference has to be before the object goes out of scope.

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

And creating a reference is called borrowing, right?

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

The owner owns the value.

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

And now we have a borrow of the value, the reference.

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

And the borrow checkout checks that the reference is borrowed.

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

Like the last use of the reference is before the object goes out of scope.

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

So if you have, say, let's say you have an immutable reference you're reading, then if you change the object, let's say it's in vector.

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

So I have an immutable reference I'm reading element five, and then I change the vector I call clear.

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

Then the borrow checker also ensures that you can't use the reference to object five afterwards.

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

Yeah, the way it works is that if there are mutable borrows, it ensures that the mutable borrow ends or starts before or after the previous borrows have ended, basically, so they don't overlap.

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

So you can only have one writer at the time, or you can have any number of readers.

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

It checks that on the scope, like in a function.

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

When you write some code that uses the object in a way that doesn't follow the rules I mentioned, that's a compiler error.

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

And fighting the borrow checker is when you can't get out of those compiler errors, I guess.

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

I think a lot of the time this has to do with the struct.

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

One common mistake I see is that if you have a struct with a reference in it, Rust kind of assumes that it can check

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

the scope of that reference by just looking at a single function.