What are zero cost abstractions in rust?

Published: 15 October 2023
on channel: Dario
861
44

If you read the rust book, you'll notice that they mention zero cost abstractions a few times.

So what are zero cost abstractions exactly?

Long story short, zero cost abstractions are higher-level features that compile to lower-level code as fast as code written manually.

A clear non-controversial example of that would be memory management.

In this example, you as a C programmer are responsible for:

allocating the memory for your array
checking that the OS actually allocated the memory.
Ensuring that you do not access elements outside of the bounds of the array.
Freeing it when you are done with it.

And this is a lot of work just to create a freaking array.

With rust, you get all that stuff for free.

No Manual Memory Management: To create a new Vector, you just call new
Bounds Checking: Rust ensures that you don't access elements out of bounds at runtime, which prevents buffer overflows.
Automatic Cleanup: When the `Vec` goes out of scope, Rust's ownership system automatically cleans up the memory, preventing any memory-related bugs.


Watch video What are zero cost abstractions in rust? online without registration, duration hours minute second in high quality. This video was added by user Dario 15 October 2023, don't forget to share it with your friends and acquaintances, it has been viewed on our site 86 once and liked it 4 people.