Upgrade to Pro — share decks privately, control downloads, hide ads and more …

RUST - THE SYSTEM LANGUAGE FOR THE FUTURE

.mateus
April 17, 2014

RUST - THE SYSTEM LANGUAGE FOR THE FUTURE

Talk by @moonbeamlabs and @seanlilmateus at cologne.rb april 2014.
Introduction to Rust

.mateus

April 17, 2014
Tweet

More Decks by .mateus

Other Decks in Programming

Transcript

  1. UST

  2. RUST: THE SYSTEM LANGUAGE FOR THE FUTURE Cologne.rb @moonbeamlabs http://github.com/moonbeamlabs

    @seanlilmateus http://github.com/seanlilmateus MATEUS ARMANDO LUCAS DOHMEN
  3. 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
  4. TDD • TDD is encouraged by Rust • A Rust

    file can contain its tests • They will only be compiled with the test flag
  5. 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 { ^~~~~~~~~~~~~~
  6. ./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 '<main>' 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") }
  7. ... 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
  8. ... let mut number:int = 10; number += 1; println!("number

    {:?}.", number); ... rustc immutable.rs && ./immutable number: 11u
  9. ... 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 }; ...
  10. ... enum Direction { North, East, South, West } ...

    enum Shape { Circle(Point, f64), Rectangle(Point, Point), } ...
  11. ... let center = Point { x:10.0, y:10.0 }; let

    radius = 50.0; let shape = Circle(center, radius); ! ...
  12. ... 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) } } ! ...
  13. ... 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 } } ...
  14. ... 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()); } ...
  15. ... 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)
 } } ...
  16. ... let square = |x: int| -> int { x

    * x }; let dois = square(1); ! let string = ~"Hello"; spawn(proc() { println!("{} world!", string); }); ...
  17. • 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.
  18. ~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.
  19. The cat has a friend. The friend wants to borrow

    the spaceship. This is possible in Rust.
  20. & 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
  21. • 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
  22. The Bad Parts • Ecosystem is really small • This

    will hopefully change with Cargo • Breaking Changes • The weirdest semicolon rule ever
  23. • "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