Introduction
Rust promises something that sounds impossible: the speed of C and C++, without their most famous class of bugs β crashes and security holes caused by bad memory handling β and without a garbage collector pausing your program to clean up.
The trick is not magic. It is a set of rules, checked by the compiler before your program ever runs, called ownership.
Ownership: every value has one owner
In Rust, every value has exactly one owner β the variable that holds it. When the owner goes out of scope, the value is cleaned up automatically. Move the value to another variable, and the first one can no longer use it.
fn main () { let s1 = String :: from ( "hello" ); let s2 = s1 ; // s1 is moved, not copied // println!("{}", s1); // Error: s1 no longer owns the value println! ( "{}" , s2 ); // Works }
Enter fullscreen mode
Exit fullscreen mode
This looks annoying at first, and it is, briefly.β¦