concepts of programming languages
play

Concepts of programming languages Rust Floris Schild, Mats - PowerPoint PPT Presentation

[ Faculty of Science Information and Computing Sciences] 1 Concepts of programming languages Rust Floris Schild, Mats Veldhuizen, Ruben Schenkuizen, Tobias van Driessel, Tom Freijsen [ Faculty of Science Information and Computing Sciences] 2


  1. [ Faculty of Science Information and Computing Sciences] 1 Concepts of programming languages Rust Floris Schild, Mats Veldhuizen, Ruben Schenkuizen, Tobias van Driessel, Tom Freijsen

  2. [ Faculty of Science Information and Computing Sciences] 2

  3. [ Faculty of Science Information and Computing Sciences] 3 Presentation Schedule ▶ Background ▶ Design principles ▶ What problems does Rust solve and how? ▶ Practical details

  4. [ Faculty of Science Information and Computing Sciences] 4 Background ▶ Personal project of Mozilla employee ▶ Sponsored by Mozilla Research ▶ First stable release (1.0) in 2015 ▶ Used in Firefox and by Dropbox ▶ Most loved programming language - SO Developer Survey

  5. [ Faculty of Science Information and Computing Sciences] 5 Quick Overview ▶ Statically typed ▶ Functional and Imperative paradigms ▶ Strict language

  6. [ Faculty of Science Information and Computing Sciences] 6 Beautiful Quotes Mozilla Research: “Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety.” “It fuses the expressive and intuitive syntax of high-level languages with the control and performance of a low-level language.”

  7. ▶ Zero-cost abstractions ▶ Move semantics ▶ Guaranteed memory safety ▶ Threads without data races ▶ Trait-based generics ▶ Pattern matching ▶ Type inference ▶ Minimal runtime ▶ Efficient C bindings [ Faculty of Science Information and Computing Sciences] 7 Features

  8. ▶ Move semantics ▶ Guaranteed memory safety ▶ Threads without data races ▶ Trait-based generics ▶ Pattern matching ▶ Type inference ▶ Minimal runtime ▶ Efficient C bindings [ Faculty of Science Information and Computing Sciences] 7 Features ▶ Zero-cost abstractions

  9. ▶ Guaranteed memory safety ▶ Threads without data races ▶ Trait-based generics ▶ Pattern matching ▶ Type inference ▶ Minimal runtime ▶ Efficient C bindings [ Faculty of Science Information and Computing Sciences] 7 Features ▶ Zero-cost abstractions ▶ Move semantics

  10. ▶ Threads without data races ▶ Trait-based generics ▶ Pattern matching ▶ Type inference ▶ Minimal runtime ▶ Efficient C bindings [ Faculty of Science Information and Computing Sciences] 7 Features ▶ Zero-cost abstractions ▶ Move semantics ▶ Guaranteed memory safety

  11. ▶ Trait-based generics ▶ Pattern matching ▶ Type inference ▶ Minimal runtime ▶ Efficient C bindings [ Faculty of Science Information and Computing Sciences] 7 Features ▶ Zero-cost abstractions ▶ Move semantics ▶ Guaranteed memory safety ▶ Threads without data races

  12. ▶ Pattern matching ▶ Type inference ▶ Minimal runtime ▶ Efficient C bindings [ Faculty of Science Information and Computing Sciences] 7 Features ▶ Zero-cost abstractions ▶ Move semantics ▶ Guaranteed memory safety ▶ Threads without data races ▶ Trait-based generics

  13. ▶ Type inference ▶ Minimal runtime ▶ Efficient C bindings [ Faculty of Science Information and Computing Sciences] 7 Features ▶ Zero-cost abstractions ▶ Move semantics ▶ Guaranteed memory safety ▶ Threads without data races ▶ Trait-based generics ▶ Pattern matching

  14. ▶ Minimal runtime ▶ Efficient C bindings [ Faculty of Science Information and Computing Sciences] 7 Features ▶ Zero-cost abstractions ▶ Move semantics ▶ Guaranteed memory safety ▶ Threads without data races ▶ Trait-based generics ▶ Pattern matching ▶ Type inference

  15. ▶ Efficient C bindings [ Faculty of Science Information and Computing Sciences] 7 Features ▶ Zero-cost abstractions ▶ Move semantics ▶ Guaranteed memory safety ▶ Threads without data races ▶ Trait-based generics ▶ Pattern matching ▶ Type inference ▶ Minimal runtime

  16. [ Faculty of Science Information and Computing Sciences] 7 Features ▶ Zero-cost abstractions ▶ Move semantics ▶ Guaranteed memory safety ▶ Threads without data races ▶ Trait-based generics ▶ Pattern matching ▶ Type inference ▶ Minimal runtime ▶ Efficient C bindings

  17. [ Faculty of Science Information and Computing Sciences] 8 Variable bindings fn main() { let y = 3; } let (a, b) = (1, 2); let mut x : u8 = 10; // Make x mutable x = 255; ▶ Use let keyword to create binding ▶ Bindings are immutable by default ▶ Lhs not a name, but a pattern ▶ Type annotations

  18. [ Faculty of Science Information and Computing Sciences] 9 Functions fn square(x: i32) -> i32 { x * x // Expression } fn printSomething() { println!("Something"); // Statement } ▶ Use fn keyword ▶ Statements vs expressions

  19. [ Faculty of Science Information and Computing Sciences] 10 Arrays and tuples Arrays can be created like this: let mut y = [4, 5, 6]; let second = y[1]; let mut z = [1; 10]; // Ten elements initialized to 1 Tuples are created as follows: let mut tuple = (1, 2); let x = tuple.0; let y = tuple.1; println!("Value of y is {}", y); let (x, y, z) = (1, 2, 3); // Destructured tuple

  20. [ Faculty of Science Information and Computing Sciences] 11 Control flow let x = 2; let y = if x == 3 { 4 } else { 5 }; loop { println!("Infinite loop"); } for x in 1..10 {} for (index, value) in (1..10).enumerate() { } let a = [1, 2, 3, 4, 5]; for elem in a.iter() { println!("the value is: {}", elem); } ▶ If , else , else if … ▶ Loops, like loop , while and for

  21. [ Faculty of Science Information and Computing Sciences] 12 Vectors let mut v = vec![1,2,3] for i in v {} // For loop takes ownership for i in &v {} // Reference for i in &mut v {} // Mutable reference ▶ Dynamic arrays ▶ Allocated on heap (as opposed to arrays)

  22. [ Faculty of Science } let point = Point { x: 3, y: 6 }; } } println!("x is {}, y is {}", self.x, self.y); fn print_xy(&self) { impl Point { point.print_xy(); // Shows x is 3, y is 6. Information and Computing Sciences] x: i32, struct Point { Structs 13 ▶ Comparable to classes. ▶ Can also have methods and associated functions y: i32,

  23. [ Faculty of Science Information and Computing Sciences] 14 Match let x: u32 = 3; match x { 1 => println!("one"), 2 => println!("two"), _ => println!("three or more"), } ▶ Comparable to switch ▶ Allows matching on expressions

  24. [ Faculty of Science Information and Computing Sciences] 15 Enums enum Choice { Milk(i32), Tea(String), } let m = Choice::Milk(20); ▶ Define type and enumerate on its variants

  25. [ Faculty of Science Information and Computing Sciences] 16 Generics fn takes_anything<T, U>(x: T, u: U) {} struct Value<T> { value : T } enum Choice<T> { Milk(T), } ▶ Generic structs, enums and functions.

  26. [ Faculty of Science } println!("This shape has an area of {}", shape.area()); fn print_area<T: HasArea>(shape: T) { } } self.side * self.side fn area(&self) -> f64 { impl HasArea for Square { fn area(&self) -> f64; Information and Computing Sciences] trait HasArea { } side: f64, struct Square { Traits 17 } ▶ Somewhat comparable to an interface ▶ Use trait bounds on generics

  27. [ Faculty of Science Information and Computing Sciences] 18 Much, much more.. ▶ Smart pointers ▶ Concurrency ▶ Closures ▶ …

  28. ▶ Automatic reference counting ▶ Ownership [ Faculty of Science Information and Computing Sciences] 19 Memory management Different systems Key difference: When are objects on the heap deallocated? ▶ Garbage collection

  29. ▶ Ownership [ Faculty of Science Information and Computing Sciences] 19 Memory management Different systems Key difference: When are objects on the heap deallocated? ▶ Garbage collection ▶ Automatic reference counting

  30. [ Faculty of Science Information and Computing Sciences] 19 Memory management Different systems Key difference: When are objects on the heap deallocated? ▶ Garbage collection ▶ Automatic reference counting ▶ Ownership

  31. [ Faculty of Science Information and Computing Sciences] 19 Memory management Different systems Key difference: When are objects on the heap deallocated? ▶ Garbage collection ▶ Automatic reference counting ▶ Ownership

  32. [ Faculty of Science Information and Computing Sciences] 20 Ownership Example let x = String::from("hello"); The variable x is the owner of the string object “hello” on the heap. The string object will be deallocated when x goes out of scope. ▶ Every value has one variable that is its owner.

  33. [ Faculty of Science Information and Computing Sciences] 20 Ownership Example let x = String::from("hello"); The variable x is the owner of the string object “hello” on the heap. The string object will be deallocated when x goes out of scope. ▶ Every value has one variable that is its owner.

  34. [ Faculty of Science Information and Computing Sciences] 21 Ownership Another example fn say_hello(name: String) { println!("Hello {}", name); } let x = String::from("Wouter"); say_hello(x); The ownership is passed into say_hello , which deallocates our string when its scope ends. say_hello(x); // Error!

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend