http://is.gd/qPAk9H

Gives error

main.rs:4:3: 4:4 error: cannot borrow `v` as mutable because it is also borrowed as immutable main.rs:4 v.push(5); ^ main.rs:3:12: 3:13 note: previous borrow of `v` occurs here; the immutable borrow prevents subsequent moves or mutable borrows of `v` until the borrow ends main.rs:3 let x = &v[0]; ^ main.rs:6:2: 6:2 note: previous borrow ends here main.rs:1 fn main() { main.rs:2 let mut v = vec![1, 2, 3, 4]; main.rs:3 let x = &v[0]; main.rs:4 v.push(5); main.rs:5 println!("v[0] is {}", x); main.rs:6 } ^

Ownership Every value has an Owner

{ let v = vec![1, 2, 3, 4]; }

There can only be one owner

owner Ownership means "right to destroy"

Values are destroyed at the end of their scope

You can transfer ownership

let v2 = v; //v is no longer valid



Borrowing You can borrow values

let v2 = &v;

Borrows cannot outlive the owner

You can have any number of shared (immutable) borrows

OR one mutable borrow

let v2 = &mut v;

Solves aliasing

How much do you trust int, *char ? This is the same in Rust &str you know it's safe

If you need more... There's plenty of "escape hatches" If ownership is shared, use Rc

If mutability is shared, use Cell or RefCell

If ownership is shared across threads, use Arc

If mutability is shared across threads, use Mutex

Note: You can't accidentally forget to use them (!) Won't compile



These are safe because they adhere to the underlying principles of the borrow checker

They move the checking from "compile-time" to "runtime"

None of these are "magic"

You can implement your own

...or use a library

They are composable Can get out of hand? Vec<Option<Rc<RefCell<T>>> roughly equivalent to C++ vector<shared_prt<T>> Java List<T>

Downsides "No Silver Bullet"

Borrow checker can be a pain sometimes...

Can be a bit too "explicit"

Compilation can be slow

No incremental yet



Improving...

Limited IDE support

No direct C++ interop

Windows support improving

Language is still fresh / "settling down"

But: New stable release every 6 weeks



So.... should we all use it? ;) It's definitely worth a try! https://www.rust-lang.org/ Great community! Book: Why Rust? (from where I stole the title of this presentation)

Live Code Demo Let's do an HTTP request