Hey, you! If you have yet to install Rust:
Run `rustc --version` to ensure that installation was successful
Ben Striegel
@bstrie
// C
int sum = 0;
for (int i=0; i<x; i++) {
sum += i;
}
# Python
sum = 0
for i in range(0, x):
sum += i
# `sum()` ist verboten
// Rust
let mut sum = 0;
for i in 0..x {
sum += i;
}
# Ruby
(0...x).inject(0) {|sum, i| sum + i}
# Python
reduce(lambda sum, i: sum + i,
range(0, x), 0)
// Rust
(0..x).fold(0, |sum, i| sum + i)
; Assembly
lea eax, [rdi - 1]
lea ecx, [rdi - 2]
imul rcx, rax
shr rcx
lea eax, [rcx + rdi - 1]
// Rust
(0..x).fold(0, |sum, i| sum + i)
// C, runtime memory error
int* foo() {
int x = 4;
return &x;
}
// Go, safe thanks to GC
func foo() *int {
x := 4
return &x
}
// Rust, does not compile
fn foo() -> &i32 {
let x = 4;
return &x;
}
// Rust, does not compile
fn foo() -> &i32 {
let x = 4;
return &x;
}
error: missing lifetime specifier [--explain E0106] --> return.rs:1:13 2 |> fn foo() -> &i32 { |> ^^^^ help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from
// C++, invalidated iterator
for(auto i: v) {
if (i > 0) {
v.push_back(i * -1);
}
}
// Java, runtime exception
for (Integer i: v) {
if (i > 0) {
v.add(i * -1);
}
}
// Rust, does not compile
for i in v {
if i > 0 {
v.push(i * -1);
}
}
error: use of moved value: `v` [--explain E0382] --> invalidated.rs:5:13 2 |> for i in v { |> - value moved here 3 |> if i > 0 { 4 |> v.push(i * -1); |> ^ value used here after move
// Go, with data race
x := 0
for i := 0; i <= 10; i++ {
go func() { x += 1 }()
}
// Go, without data race
mutex := &sync.Mutex{}
x := 0
for i := 0; i <= 10; i++ {
go func() {
mutex.Lock()
x += 1
mutex.Unlock()
}()
}
// Rust, does not compile
let x = 0;
for i in 0..10 {
spawn(|| x += 1);
}
error: closure may outlive the current function, but it borrows `x`, which is owned by the current function [--explain E0373] --> race.rs:4:15 4 |> spawn(|| x += 1); |> ^^ note: `x` is borrowed here --> race.rs:4:18 4 |> spawn(|| x += 1); |> ^
// Rust
let x = Arc::new(Mutex::new(0));
for i in 0..10 {
let y = x.clone();
spawn(move || {
let mut z = y.lock().unwrap();
*z += 1;
});
}
Things to note:
fn main() {
println!("Hello!");
}
// C
while (x) y;
for (w; x; y;) z;
for (;;) x;
if (u)
v;
else if (w) {
x;
y;
} else
z;
// Rust
while x { y; }
for x in y { z; }
loop { x; }
if u {
v;
} else if w {
x;
y;
} else {
z:
}
Signed fixed-width integers:
Unsigned fixed-width integers:
Platform-sized integers:
Floating point:
UTF-32 Unicode scalar values:
Booleans:
i8, i16, i32, i64 u8, u16, u32, u64 isize, usize f32, f64 char bool
Fixed-size array of N elements:
Growable, heap-allocated buffer:
Growable, heap-allocated buffer of UTF-8:
Tuple containing several different types:
[T; N] Vec<T> String (T, U, V)
See the `collections` module in the standard library for hashmaps, sets, B-trees, and more.
Shared reference:
Mutable reference:
Shared slice (pair of pointer and length):
Mutable slice:
String slice:
Heap-allocated smart pointer:
Reference-counted smart pointer:
&T &mut T &[T] &mut [T] &str Box<T> Rc<T>
let a = 1; // integer
let b = 2.0; // float
let c = "three"; // static string slice
let d = '4'; // char
let e = [5, 6, 7]; // fixed-size array
let f = ("eight", 9.0); // tuple
let g = vec![10, 11, 12]; // vector
Create new library project:
Create new application project:
Build and run in debug mode:
Build and run in release mode:
Build without running:
Run your test suite:
Update dependencies:
View command-line options:
cargo new foo cargo new foo --bin cargo run cargo run --release cargo build cargo test cargo update cargo help
Get the code at github.com/jimblandy/exercises
If you have git:
git clone https://github.com/jimblandy/exercises.git
If you don't have git, download and unzip:
https://github.com/jimblandy/exercises/archive/master.zip
Navigate into the exercises/ex1 directory and run `cargo test`
Observe the test failures
Edit the `is_prime` function in src/lib.rs until all tests pass
OPTIONAL: set up Rust support for your favorite code editor by perusing www.rust-lang.org/ides
1. All variables are owned by their enclosing scope
2. When a scope ends, everything it owns is deallocated
3. When one variable is assigned to another variable, the original variable becomes inaccessible, because a piece of memory must have only one owner at a time
4. Passing a variable to a function will move ownership of that data into that function's scope, and make the original variable inaccessible
5. Returning a value from a function or scope will move ownership of that data into the enclosing scope
1. All variables are owned by their enclosing scope
fn foo() { // this scope owns x and z
let x = Foo;
if true { // this scope owns y
let y = Bar;
}
let z = Qux;
}
2. When a scope ends, everything it owns is deallocated
fn foo() {
let x = Foo;
if true {
let y = Bar;
} // y is deallocated
let z = Qux;
} // x and z are deallocated
3. When one variable is assigned to another variable, the original variable becomes inaccessible, because a piece of memory must have only one owner at a time
let x = Foo;
let y = x;
// COMPILATION ERROR BELOW
do_anything_with(x);
4. Passing a variable to a function will move ownership of that data into that function's scope, and make the original variable inaccessible
let x = Foo;
now_ur_mine(x);
// COMPILATION ERROR BELOW
let z = x;
5. Returning a value from a function or scope will move ownership of that data into the enclosing scope
fn bar() -> Foo {
let x = Foo;
return x;
}
let y = bar();
// Putting it all together...
fn bar(y: Foo) -> Foo {
return y;
}
fn qux(z: Foo) {
// end of the line
}
let v = Foo; // memory allocated here
let w = v;
let x = bar(w);
qux(x);
There is one and only one way to alter the behavior of ownership: by making a type copyable
Once a type is copyable, moves will no longer render the original variable inaccessible
This is the complete extent of the user's ability to hijack the ownership system itself, i.e. there are no user-defined copy constructors as there are in C++
This mechanism exists because there's really no benefit to treating small, boring data types like booleans or integers as uniquely-owned pieces of data
// Traits as interfaces
trait DoesSomething {
fn do_something(self);
}
impl DoesSomething for i32 {
fn do_something(self) {
println!("Extending basic types!");
}
}
let x = 6;
x.do_something();
fn not_generic(x: i32) -> i32 {
return x;
}
fn totes_generic<T>(x: T) -> T {
return x;
}
// Traits as bounds on generics
trait DoesSomething {
fn do_something(self);
}
fn kinda_useless_generic<T>(x: T) {
// we basically can't do anything with x,
// because we don't know what it allows!
}
fn kinda_useful_generic<T: DoSomething>(x: T)
{
x.do_something();
}
impl DoesSomething for i32 {
fn do_something(self) { println!("OMG!"); }
}
impl DoesSomething for bool {
fn do_something(self) { println!("ZOMG!!"); }
}
fn kinda_useful_generic<T: DoSomething>(x: T) {
x.do_something();
}
let y = 9;
let z = true;
kinda_useful_generic(y);
kinda_useful_generic(z);
struct Foo {
w: i32,
x: usize
}
struct Bar<T, U> {
y: T,
z: U
}
struct Foo<T> {
x: T,
y: i32
}
fn spam() -> i32 { return 5; }
let bar = Foo { x: 1.0, y: 2 };
let qux = Foo { y: 3, x: spam };
println!("{}", bar.y);
println!("{}", (qux.x)());
use SpellCast::{Fireball, BalefulPolymorph};
enum SpellCast {
Fireball,
BalefulPolymorph
}
let x = Fireball;
match x {
Fireball => println!("BOOM!!"),
BalefulPolymorph => println!("MOO!!")
}
use IntList::{Node, Empty};
enum IntList {
Node(i32, Box<IntList>),
Empty
}
let x = Node(2,
Box::new(Node(4,
Box::new(Empty))));
use Option::{Some, None};
// Redundant with the stdlib
enum Option<T> {
Some(T),
None
}
fn foo<T>(x: Option<T>) {
match x {
Some(y) => println!("Something!"),
None => println!("Nothing :((("),
}
}
#[derive(Debug)]
enum SpellCast {
Fireball,
BalefulPolymorph
}
fn main() {
let x = SpellCast::Fireball;
println!("{:?}", x);
fn main(x: Foo) -> Foo {
// man, really wish I
// didn't have to return x...
}
fn main(x: &Foo) {
// sheer ecstasy!
}
Literally the hardest thing about learning Rust as a newcomer! The dreaded Borrow Checker!! :O
SECRET PRO TIP: The borrow checker only kicks in if you're using references, or are using something that might eventually hold references. If you don't want to deal with borrow check errors, then you can just clone and box everything until you feel comfortable.
DOUBLE SECRET PRO TIP: Download the nightly compiler and pass the experimental flag for new borrow check error message!
TRIPLE SECRET PRO TIP: Just ask on IRC! They're all seasoned borrow check veterans.
Get these slides at slides.com/bstrie/rust-oscon