What are zero cost abstractions in rust? #rustprogramming #rustlang
Security Union
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.
...
https://www.youtube.com/watch?v=Lw-e3CDzE64
2577001 Bytes