Rust-lang

2,197 views

Published on

Rust
Why do you care about Rust? Who has the time to learn all these new languages? It seems like a new one is popping up every other week and this trend is growing at an exponential rate. Good news, a fair number of them are crafted really well and efficiently solve specific problems. Bad news, how do you keep up with all of this, let alone decide which languages to include in your companies technology portfolio.

Despite the challenges of all these new languages, a majority of developers are intrigued about the idea of becoming a polyglot, but don't know where to begin or don't have the time. In my polyglot travels, there is one language of late that is the sure-fire answer to the above questions, Rust.

In this talk I’ll explore the value behind becoming more polyglotic as a developer, how to pick languages to learn, and then dive deep in the the language of Rust. Which in my opinion, is hands down the best up and coming languages to learn.

About the Presenter
Anthony Broad-Crawford has been a developer since the year 2000 with a short side stint as a semi-professional poker player. Since his transition to software development Anthony has...

1. Built 8 patent receiving technologies
2. Founded two global companies
3. Been a CTO (3x), CPO (1x), and CEO (1x)

and is currently the CTO at Fooda where he manages product, user experience, and engineering. Fooda is predominantly web and mobile technology company focused on bringing great & healthy food from the best restaurant's to people while at the office.

Through his career, in production applications Anthony has used Ruby, Java, Jave (Android), Objective-C and Swift, .NET, Erlang, Scala, Node.JS, LISP, Smalltalk, and even assembly, with his recent favorite, Rust . No, not all at the same time in the same application.

Anthony now spends his time building great teams, that leverage great technology, to build great products, but still looks to codes every chance he can get :)

Published in: Engineering
1 Comment
9 Likes
Statistics
Notes
  • From slide 20 to 23, there's a string end error
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
2,197
On SlideShare
0
From Embeds
0
Number of Embeds
187
Actions
Shares
0
Downloads
45
Comments
1
Likes
9
Embeds 0
No embeds

No notes for slide

Rust-lang

  1. 1. Rust
  2. 2. Anthony Broad-Crawford CTO @ Fooda
  3. 3. Rust is a systems programming language that runs blazingly fast, prevents almost all crashes*, and eliminates data races.
  4. 4. rust 1. guaranteed memory safety 2. threads without dataraces 3. zero-cost abstractions (done at compile time) 4. trait-based generics 5. pattern matching 6. type inference 7. & more
  5. 5. Cargo is a tool that allows Rust projects to declare their various dependencies, and ensure that you'll always get a repeatable build.
  6. 6. and tests ...
  7. 7. cargo 1. introduces conventions over configuration 2. declarative syntax for dependencies 3. declarative syntax for compilation profiles 4. easy execution of unit, integration, & benchmark tests
  8. 8. our obligatory hello world fn main() { println!("Hello, world!"); } //> Cargo run //> Hello, world! //
  9. 9. let's go create a hello world with Cargo!
  10. 10. Using cargo to create projects Rust's package manager Usage: cargo <command> [<args>...] cargo [options] Options: -h, --help Display this message -V, --version Print version info and exit --list List installed commands -v, --verbose Use verbose output Some common cargo commands are: build Compile the current project clean Remove the target directory doc Build this project's and its dependencies' documentation new Create a new cargo project run Build and execute src/main.rs test Run the tests bench Run the benchmarks update Update dependencies listed in Cargo.lock
  11. 11. Primative types, Memory safety & zero cost abstractions
  12. 12. primatives fn main() { //integers let i: i8 = 1; // i16, i32, i64, and i are available //unsigned let u: u8 = 2; // u16, u32, u64, and u are available //floats let f: f32 = 1.0; // f64 also available //booleans let b: bool = true; // false also available, duh //string and characters let c: char = 'a'; let s: &str = "hello world"; }
  13. 13. variable bindings fn main() { let x: int = 1; //explicitly declare type let y = 2i; //type inference let (a,b,c) = (1i,2i,3i); //variable declaration via patterns let a = [1, 2, 3]; //array literals let s = "hello"; //string literal println!("*_^ x = {}, y = {}, a,b,c = {},{},{}", x,y,a,b,c); } //> Cargo run //> *_^ x = 1, y = 2, a,b,c = 1,2,3 //
  14. 14. variable mutability fn main() { let x: int = 1; x = 10; println!("The value of x is {}", x); } //Cargo run //error: re-assignment of immutable variable `x` // x = 10; // ^~~~~~~
  15. 15. variable mutability fn main() { let mut x: int = 1; x = 10; println!("The value of x is {}", x); } //Cargo run //warning: value assigned to `x` is never read // let mut x: int = 1; // ^~~~~~~
  16. 16. The Rust compiler is SUPER helpful fn main() { let mut x: int = 1; x = 10; println!("The value of x is {}", x); } //Cargo run //warning: value assigned to `x` is never read // let mut x: int = 1; // ^~~~~~~
  17. 17. stack vs. heap fn main() { let y: int = 1; //allocated on the stack let x: Box<int> = Box::new(10); //allocated on the heap println!("Heap {}, Stack {}", x, y); } //> Cargo run //> Heap 10, Stack 1 //
  18. 18. heap allocation creates pointers fn main() { let x: Box<int> = box 10; //allocated on the heap x = 11; println!("The Heaps new value is {}, x"); } //Cargo run //error: mismatched types: expected `Box<int>` // x = 11; // ^~~~~~~
  19. 19. memory mutability fn main() { let x: Box<int> = box 10; //allocated on the heap *x = 11; println!("The Heaps new value is {}, x"); } //Cargo run //error: cannot assign to immutable dereference of `*x` // x = 11; // ^~~~~~~
  20. 20. memory mutability fn main() { let mut x: Box<int> = box 10; //allocated on the heap *x = 11; println!("The Heaps new value is {}, x"); } //> Cargo run //> The Heaps new value is 11 //
  21. 21. compiler protects you by owning de-allocation fn main() { let mut x: Box<int> = box::new(10); //allocated on the heap *x = 11; println!("The Heaps new value is {}, x"); //Scope for x ends here so the compiler adds the de-allocation //free(x); } //> Cargo run //> The Heaps new value is 11 //
  22. 22. There is no garbage collection in Rust. The compiler observes the lifetime of a variable and de-allocates it where it is no longer used.
  23. 23. but these features don't sum to the promised memory safety
  24. 24. borrowing fn main() { // x is the owner of the integer, which is memory on the stack. let x = 5; // you may lend that resource to as many borrowers as you like let y = &x; let z = &x; // functions can borrow too! foo(&x); // we can do this alllllll day! let a = &x; }
  25. 25. ownership is singular fn main() { let mut x = 5; let y = &mut x; //mutability only allows one borrower let z = &mut x; // ... no go } //> Cargo build //> error: cannot borrow `x` as mutable more than once at a time // let z = &mut x; // ^~~~~~~~~~~~~~~
  26. 26. ownership can be transferred fn main() { let x: Box<i32> = Box::new(5); let y = add_one(x); println!("{}", y); } fn add_one(mut num: Box<i32>) -> Box<i32> { *num += 1; num } //> Cargo run //> 6 //
  27. 27. ownership can be transferred back fn main() { let mut x: Box<i32> = Box::new(5); x = add_one(x); println!("{}", x); } fn add_one(mut num: Box<i32>) -> Box<i32> { *num += 1; num } //> Cargo run //> 6 //
  28. 28. now THAT sums to to the promised memory safety and data race prevention
  29. 29. Other features of Rust
  30. 30. pattern matching fn main() { let x = 5; match x { 1 => println!("one"), 2 => println!("two"), 3 => println!("three"), 4 => println!("four"), 5 => println!("five"), _ => println!("something else"), } }
  31. 31. pattern matching fn main() { let x = 5; match x { 1 | 2 => println!("one or two"), 3 => println!("three"), 4 ... 7 => println!("4 through 7"), _ => println!("anything"), } }
  32. 32. pattern matching fn main(){ struct Point { x: i32, y: i32, } let origin = Point { x: 0, y: 0 }; match origin { Point { x: x, .. } => println!("x is {}", x), } } //> Cargo run //> x is 0 //
  33. 33. iterators & adapters fn main(){ for i in range(1i, 10i).filter(|&x| x % 2 == 0) { println!("{}", i); } } //> Cargo run //> 2 //> 4 //> 6 //> 8
  34. 34. functions //a function that takes and integer and returns an integer fn add_one(x: i32) -> i32 { x + 1 } fn main(){ println!("1 plus 1 is {}", add_one(1)); } //> Cargo run //> 1 plus 1 is 2 //
  35. 35. closures fn main() { let add_one = |x| { 1 + x }; println!("The sum of 5 plus 1 is {}.", add_one(5)); } //> Cargo run //> The sum of 5 plus 1 is 6. //
  36. 36. closures can be passed as params //Generic function that takes a closure as an argument fn twice<F: Fn(i32) -> i32>(x: i32, f: F) -> i32 { f(x) + f(x) } fn main() { let result = twice(5, |x: i32| { x * x }); println!("And we have .... {}", result); } //> Cargo run //> And we have .... 50 //
  37. 37. traits struct Circle { x: f64, y: f64, radius: f64, } trait HasArea { fn area(&self) -> f64; } impl HasArea for Circle { fn area(&self) -> f64 { std::f64::consts::PI * (self.radius * self.radius) } } fn main() { let c = Circle {x:0.0, y:1.0, radius: 2.0}; println!("The circles's radious is {}", c.area()); }
  38. 38. traits with generics trait HasArea { fn area(&self) -> f64; } fn print_area<T: HasArea>(shape: T) { println!("This shape has an area of {}", shape.area()); } fn main() { let c = Circle {x:0.0, y:1.0, radius: 2.0}; print_area(c); } //> Cargo run //> This shape has an area of 12.566371 //
  39. 39. So ... no classes?
  40. 40. concurrency fn print_message(){ println!("Hello from within a thread!"); } fn main() { spawn(print_message); } //> Cargo run //> Hello from within a thread! //
  41. 41. concurrency and ownership fn print_message(){ println!("Hello from within a thread!"); } fn main() { let x: int = 5; spawn(move || print_message); x = 10; } //> Cargo run //> error: re-assignment of immutable variable `x` // x = 10; // ^~~~~~~
  42. 42. The promised land of the Rust compiler :)
  43. 43. Cargo also has support for testing 1. unit testing 2. integration testing 3. benchmark testing
  44. 44. testing #[test] fn adding_one(){ let expected: int = 5; let actual: int = 4; assert_eq!(expected,actual); } //> Cargo Test //> running 1 tests //> test adding_one ... FAILED //
  45. 45. testing an expected failure #[test] #[should_fail] fn adding_one(){ let expected: int = 5; let actual: int = 4; assert_eq!(expected,actual); } //> Cargo Test //> running 1 tests //> test adding_one ... OK //
  46. 46. making it pass #[test] fn adding_one(){ let expected: int = 5; let actual: int = 5; assert_eq!(expected,actual); } //> Cargo Test //> running 1 tests //> test adding_one ... OK //
  47. 47. benchmark tests #[bench] fn bench_add_two(b: &mut Bencher) { let add_one = |x| { 1 + x }; b.iter(|| add_one(2)); } //> Cargo Test //> running 1 tests //> test tests::bench_add_two ... bench: 1 ns/iter (+/- 0) //> test result: ok. 0 passed; 0 failed; 0 ignored; 1 measured //
  48. 48. As promised Rust 1. guarantees memory safety 2. threads without dataraces 3. zero-cost abstractions (done at compile time) 4. trait-based generics 5. pattern matching 6. type inference 7. & more
  49. 49. additional links 1. http://www.rust-lang.org —Guide (nice intro) —Reference (deeper articles) 2. http://rustbyexample.com 3. irc (super friendly and helpful lot)
  50. 50. lastly curl -s https://static.rust-lang.org/rustup.sh | sudo sh
  51. 51. Questions?
  52. 52. Anthony Broad-Crawford @broadcrawford abc@anthonybroadcraword.com 650.691.5107 (c)

×