Ce diaporama a bien été signalé.
Le téléchargement de votre SlideShare est en cours. ×

Introduction to rust: a low-level language with high-level abstractions

Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Prochain SlideShare
ECMAScript 2015 Tips & Traps
ECMAScript 2015 Tips & Traps
Chargement dans…3
×

Consultez-les par la suite

1 sur 17 Publicité
Publicité

Plus De Contenu Connexe

Diaporamas pour vous (20)

Similaire à Introduction to rust: a low-level language with high-level abstractions (20)

Publicité

Plus récents (20)

Publicité

Introduction to rust: a low-level language with high-level abstractions

  1. 1. Rust a low-level language with high-level abstractions
  2. 2. Hello, world! $ cargo new --bin helloworld && cd helloworld $ cargo run Compiling helloworld v0.1.0 Running `target/debug/helloworld` Hello, world!
  3. 3. $ cargo build --release Compiling helloworld v0.1.0rust/helloworld $ ./target/release/helloworld Hello, world!
  4. 4. Low level • C-like performances • no VM • no garbage collector
  5. 5. High level fn is_odd(n: u32) -> bool {
 n % 2 == 1
 }
 
 fn main() {
 println!("Find the sum of all the squared odd numbers under 1000");
 let upper = 1000;
 let sum_of_squared_odd_numbers: u32 =
 (0..).map(|n| n * n) // All natural numbers squared
 .take_while(|&n| n < upper) // Below upper limit
 .filter(|n| is_odd(*n)) // That are odd
 .fold(0, |sum, i| sum + i); // Sum them
 println!("result: {}", sum_of_squared_odd_numbers);
 } Gist
  6. 6. High level • types • type inference • no manual memory management • no null -> Option<T> • no exception -> Result<T>
  7. 7. High level • pattern matching • closure • immutable by default • functional
  8. 8. Abstraction without overhead let mut acc = 0;
 // Iterate: 0, 1, 2, ... to infinity
 for n in 0.. {
 let n_squared = n * n;
 
 if n_squared >= upper {
 // Break loop if exceeded the upper limit
 break;
 } else if is_odd(n_squared) {
 acc += n_squared;
 }
 }
 acc let sum_of_squared_odd_numbers: u32 =
 (0..).map(|n| n * n)
 .take_while(|&n| n < upper)
 .filter(|n| is_odd(*n))
 .fold(0, |sum, i| sum + i); Same perf! (functional version even faster)
  9. 9. Not OO but Struct with impl struct Person {
 name: String,
 }
 
 impl Person {
 pub fn say_hello(&self) {
 println!("{} says hello!", self.name);
 }
 }
 
 fn main() {
 let p = Person { name: "Bibi".to_string() };
 p.say_hello();
 } Gist
  10. 10. Typeclasses #[derive(Debug)]
 struct Age {
 age: i32,
 }
 
 fn main() {
 let age1 = Age { age: 12 };
 let age2 = Age { age: 24 };
 let sum = age1 + age2;
 println!("age1 + age2 = {:?}", sum);
 } <anon>:9:15: 9:19 error: binary operation `+` cannot be applied to type `Age` [E0369] <anon>:9 let sum = age1 + age2; ^~~~
  11. 11. Typeclassesuse std::ops::Add;
 
 #[derive(Debug)]
 struct Age {
 age: i32,
 }
 
 impl Add for Age {
 type Output = Age;
 
 fn add(self, other: Age) -> Age {
 println!("Adding!");
 Age { age: self.age + other.age }
 }
 }
 
 fn main() {
 let age1 = Age { age: 12 };
 let age2 = Age { age: 24 };
 let sum = age1 + age2;
 println!("age1 + age2 = {:?}", sum);
 } Gist
  12. 12. Program structure • Cargo.toml • unit tests next to implementation • integration tests in /tests
  13. 13. Ecosystem • cargo and crates.io • dependency management • build / cross-compilation • https://play.rust-lang.org/ • very welcoming community • https://github.com/kud1ing/awesome-rust
  14. 14. Documentation • https://doc.rust-lang.org/book/ 
 http://rustbyexample.com/ • https://this-week-in-rust.org/
  15. 15. Projects using Rust • Servo • Redox • Firefox mp4 • Dropbox But Go’s “memory footprint” was too high for the massive storage systems the company was trying to build. Dropbox needed a language that would take up less space in memory, because so much memory would be filled with all those files streaming onto the machine. So, in the middle of this two-and-half-year project, they switched to Rust on the Diskotech machines. And that’s what Dropbox is now pushing into its data centers.
  16. 16. Web • http://www.arewewebyet.org/ • Ex: https://tech.zalando.de/blog/getting-started- with-rust/
  17. 17. Rust • young language: 1.0 in May 2015 • very interesting • first steps can be challenging • compared to JVM: no GC / no JIT

×