Slide 1

Slide 1 text

UST

Slide 2

Slide 2 text

RUST: THE SYSTEM LANGUAGE FOR THE FUTURE Cologne.rb @moonbeamlabs http://github.com/moonbeamlabs @seanlilmateus http://github.com/seanlilmateus MATEUS ARMANDO LUCAS DOHMEN

Slide 3

Slide 3 text

Can I haz a Haskell for the real world

Slide 4

Slide 4 text

DO WE REALLY NEED ANOTHER LANGUAGE? Safety 0 1 2 3 4 5 Control 0 1 2 3 4 5 C and C++ Java Ruby & Python Haskell

Slide 5

Slide 5 text

use std::io::println; ! fn main() { println("Hello, world."); } rustc hello_world.rs ./hello_world

Slide 6

Slide 6 text

TDD • TDD is encouraged by Rust • A Rust file can contain its tests • They will only be compiled with the test flag

Slide 7

Slide 7 text

fn add(first_summand: int, second_summand: int) -> int { return 1; } ! #[test] fn test_adding_two_zeros() { assert!(add(0,0) == 0, "0 plus 0 should be 0") } rustc --test tdd.rs rustc --test tdd.rs && ./tdd tdd.rs:1:8: 1:21 warning: unused variable: `first_summand`, #[warn(unused_variable)] on by default tdd.rs:1 fn add(first_summand: int, second_summand: int) -> int { ^~~~~~~~~~~~~ tdd.rs:1:28: 1:42 warning: unused variable: `second_summand`, #[warn(unused_variable)] on by default tdd.rs:1 fn add(first_summand: int, second_summand: int) -> int { ^~~~~~~~~~~~~~

Slide 8

Slide 8 text

./tdd.rs running 1 test test test_adding_two_zeros ... FAILED ! failures: ! ---- test_adding_two_zeros stdout ---- task 'test_adding_two_zeros' failed at '0 plus 0 should be 0', tdd.rs:7 ! ! failures: test_adding_two_zeros ! test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured ! task '' failed at 'Some tests failed', /private/tmp/rust-Q8uz/rust-0.10/src/libtest/lib.rs:215 fn add(first_summand: int, second_summand: int) -> int { return 1; } ! #[test] fn test_adding_two_zeros() { assert!(add(0,0) == 0, "0 plus 0 should be 0") }

Slide 9

Slide 9 text

Immutability... Everywhere!

Slide 10

Slide 10 text

... let number:int = 10; number += 1; println!("number {:?}.", number); ... rustc immutable.rs && ./immutable immutable.rs:5:5: 5:11 error: re-assignment of immutable variable `number` immutable.rs:5 number += 1; ^~~~~~ immutable.rs:4:9: 4:15 note: prior assignment occurs here immutable.rs:4 let number:uint = 10; ^~~~~~ error: aborting due to previous error

Slide 11

Slide 11 text

... let mut number:int = 10; number += 1; println!("number {:?}.", number); ... rustc immutable.rs && ./immutable number: 11u

Slide 12

Slide 12 text

16 6 10 4 2 2 4 6 Data Structures & Enum

Slide 13

Slide 13 text

... struct Point { x:f64 ,y:f64 }; struct Unit( int, ~str ); ... let pt:Point = Point { x:0.0, y:10.0 }; let cm = Unit( 10, ~"cm"); ! let pt2 = Point { x:10.0, ..pt }; ...

Slide 14

Slide 14 text

... enum Direction { North, East, South, West } ... enum Shape { Circle(Point, f64), Rectangle(Point, Point), } ...

Slide 15

Slide 15 text

Pattern Matching

Slide 16

Slide 16 text

... let center = Point { x:10.0, y:10.0 }; let radius = 50.0; let shape = Circle(center, radius); ! ...

Slide 17

Slide 17 text

... let center = Point { x:10.0, y:10.0 }; let radius = 50.0; let shape = Circle(center, radius); ... ! fn area(sh: Shape) -> f64 { match sh { Circle(_, r) => f64::consts::PI * r * r, Rectangle(p1, p2) => (p2.x - p1.x) * (p1.y - p2.y) } } ! ...

Slide 18

Slide 18 text

Traits

Slide 19

Slide 19 text

... struct Circle { radius: f32, center: Point } trait Shape { fn area() -> f32 } ! ! impl Shape for Circle { fn area(&self) -> f32 { f32::consts::PI * self.size * self.size } } ...

Slide 20

Slide 20 text

The Power of Traits

Slide 21

Slide 21 text

... extern mod active_support; use active_support::Period; use active_support::Time; ! fn main() { let time = Time::now(); println!("{:?}", time); println!("{:?}", 2.days().from_now()); println!("{:?}", 2.weeks().from_now()); println!("{:?}", 2.months().from_now()); println!("{:?}", 2.years().from_now()); } ...

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

... use std::num::{pow, sqrt};
 struct Point { x: int, y: int }
 struct Line { a: Point, b: Point } ! impl Line { fn length(&self) -> uint { let x = pow(self.b.x - self.a.x, 2);
 let y = pow(self.b.y - self.a.y, 2);
 sqrt(x + y)
 } } ...

Slide 24

Slide 24 text

Java 8 Released Just got closures

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

... let square = |x: int| -> int { x * x }; let dois = square(1); ! let string = ~"Hello"; spawn(proc() { println!("{} world!", string); }); ...

Slide 27

Slide 27 text

Approach to Concurrency Approach to Memory

Slide 28

Slide 28 text

Memory

Slide 29

Slide 29 text

• There is a lot of fear of pointers • But this is mainly, because of pointer arithmetic and security problems • Oh, those security problems... • ...and bugs.

Slide 30

Slide 30 text

~I haz spaceship The cat has a pointer to its spaceship. The cat owns the spaceship. It can do whatever it wants with the spaceship. There is only one owner.

Slide 31

Slide 31 text

The cat has a friend. The friend wants to borrow the spaceship. This is possible in Rust.

Slide 32

Slide 32 text

& The owner: • Can't borrow it to someone else. • It can't free the pointer Borrowed The borrower: • Can't borrow it to someone else. There can't be a dangling pointer

Slide 33

Slide 33 text

Concurrent & Parallel by design

Slide 34

Slide 34 text

• Mutable portions are not allowed to be shared between tasks. • Each Task has its own its own stack, and a heap. • When a Task sends a value that has the Send Trait, it loses ownership of the value sent and can no longer refer to it Channel Communication

Slide 35

Slide 35 text

The Bad Parts • Ecosystem is really small • This will hopefully change with Cargo • Breaking Changes • The weirdest semicolon rule ever

Slide 36

Slide 36 text

• "Puzzle Sandwich" by Melissa • https://www.flickr.com/photos/buzzymelibee/8690774332 • active_support • http://words.steveklabnik.com/rust-is-surprisingly-expressive • Rust for Rubyists (by Steve Klabnik) • http://www.rustforrubyists.com • "Pointer in the mountains" by Oleh Slobodeniuk • https://www.flickr.com/photos/ualucky/12307340694