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

But maybe you're writing some super high performance code and you want to avoid this if.

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

What I would say here is that vector has two different ways of getting a value.

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

There's the normal bracket operator we all use for other languages, which will do the check and crash your program if you get it wrong.

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

But there's another function called getUnchecked.

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

And so if you do write vec.getUnchecked 5, then it will give you element 5 without checking the length.

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

And this function, like in the function signature, it says unsafe FN.

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

That's the function signature.

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

And so you can only call this function from an unsafe block.

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

And so all that the unsafe block lets you do is call functions marked unsafe.

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

So usually it will never show up in, say, a backend server.

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

You would have zero uses of unsafe there.

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

Generally, when unsafe is used, it's to add a new feature to the language.

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

Let's say the language didn't have a vector.

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

The language still has a function to allocate memory, a function to free memory, a function to read at this point or address.

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

then you could write struct vec the pointer is here and this is a raw pointer so it's unsafe to use the length is this the capacity is this so i might and then you can write your little api of course the fields are private so you can't access them from outside the module i mean if i could do vector length equal 20 just access the field that would be pretty bad and so

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

You can write your own vector API and using field privacy and good API design, you can add a vector to the language if it doesn't already exist.

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

And unsafe, if encapsulated properly in this kind of API that doesn't permit you to mess it up, then you can have a safe vector that you can use from safe code.

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

And no matter how stupid the thing you do with that vector is, it's not going to do something bad.

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

It's just going to crash or whatever, check the length properly.

The Pragmatic Engineer
Why Rust is different, with Alice Ryhl

As long as you design your API right, you can add new language features by using unsave.