Konubinix' opinionated web of thoughts

Rust Memory Handling

fleeting

Deref coercion is a convenience that Rust performs on arguments to functions and methods

https://doc.rust-lang.org/book/ch15-02-deref.html

Deref coercion works only on types that implement the Deref trait.

https://doc.rust-lang.org/book/ch15-02-deref.html

Note that f32 and f64 do not implement Hash, likely because floating-point precision errors would make using them as hashmap keys horribly error-prone

https://doc.rust-lang.org/rust-by-example/std/hash/alt_key_types.html

A String is stored as a vector of bytes (Vec<u8>), but guaranteed to always be a valid UTF-8 sequence. String is heap allocated, growable and not null terminated

https://doc.rust-lang.org/rust-by-example/std/str.html

&str is a slice (&[u8]) that always points to a valid UTF-8 sequence, and can be used to view into a String, just like &[T] is a view into Vec<T>.

https://doc.rust-lang.org/rust-by-example/std/str.html

Box, Vec, String, File, and Process are some examples of types that implement the Drop trait to free resources. The Drop trait can also be manually implemented for any custom data type.

https://doc.rust-lang.org/rust-by-example/trait/drop.html

When your code calls a function, the values passed into the function (including, potentially, pointers to data on the heap) and the function’s local variables get pushed onto the stack. When the function is over, those values get popped off the stack.

https://doc.rust-lang.org/book/ch04-01-what-is-ownership.html

We now have two variables, x and y, and both equal 5. This is indeed what is happening, because integers are simple values with a known, fixed size, and these two 5 values are pushed onto the stack.

https://doc.rust-lang.org/book/ch04-01-what-is-ownership.html

Rust has a special annotation called the Copy trait that we can place on types like integers that are stored on the stack (we’ll talk more about traits in Chapter 10). If a type implements the Copy trait, an older variable is still usable after assignment. Rust won’t let us annotate a type with the Copy trait if the type, or any of its parts, has implemented the Drop trait. If the type needs something special to happen when the value goes out of scope and we add the Copy annotation to that type, we’ll get a compile-time error.

https://doc.rust-lang.org/book/ch04-01-what-is-ownership.html

type that implements Copy must also implement Clone, because a type that implements Copy has a trivial implementation of Clone that performs the same task as Copy.

https://doc.rust-lang.org/book/appendix-03-derivable-traits.html

Notes linking here