Cool Rust Tricks

Rust is built on the idea of "Zero Cost Abstractions". This means that unlike in Java, if you add a wrapping layer or extra traits, this won't add any overhead

So you can define:

#![allow(dead_code)]
#![allow(unused)]
struct AppendOnlyVec<T> {
    inner: Vec<T>,
}
impl<T> AppendOnlyVec<T> {
    fn new(v: Vec<T>) -> AppendOnlyVec<T> {
        AppendOnlyVec{ inner: v }
    }
    fn push(&mut self, new_element: T) {
        self.inner.push(new_element);
    }
    fn into_vec(self) -> Vec<T> {
        self.inner
    }
}
fn main() {}

This type wraps a vec (by taking ownership of it so that it cannot be modified elsewhere). At compile time this struct will get inlined, as will all of the method implementations. After complication the only thing left is primitives and gotos. Exactly as though everything were written in hand optimized assembly.

So the above example and other similar abstraction can be added to clarify code without any overhead.

Compared to Java this model allows Rust to have very low memory use. For example when Tilde started using Rust their memory use decreased by 99%.

Unsafe

TODO

  • Unsafe Rust is still saver than C.
    • Ownership, borrowing, and types are all still enforced.
    • You can perform casts, write to raw memory, call CPU instructions, and call C code.
  • Normal pattern is to isolate unsafe code and wrap it in a safe interface.
    • Add lots of tests to validate what the compiler cannot.
  • Example: split_at_mut()