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

What is Rust?

What is Rust?

You may have seen Rust on Hacker News, Reddit or Twitter, but what is all the hype about? With Rust just recently having become stable, now’s a great time to learn more about this emerging language that is taking on old stalwarts like C++ without giving up on the expressiveness of higher level languages.

In this talk, Yehuda will talk about Rust’s ownership system, which provides automatic memory management without paying the runtime cost of a garbage collector or reference counting. Instead, Rust guarantees, at compile time, that your program will never segfault. Learn how that can work, and how the same approach makes your Rust code remarkably resilient against memory leaks.

Whether you’re a grizzled systems programmer or a dynamic language programmer looking to dip your toe in, this talk will have something for you.

Yehuda Katz

April 07, 2015
Tweet

More Decks by Yehuda Katz

Other Decks in Technology

Transcript

  1. What is Systems Programming? • Programming without a GC •

    Programming directly against the metal, without abstraction costs • Pay-as-you-go, lightweight runtime, or no runtime • Zero-cost abstractions • When "it doesn't end up mattering" isn't true
  2. That Doesn't Need to Mean • malloc and free •

    Assembly Language • Targeting Unix-only or Windows-only • Old-school tools like hand-crafted Makefiles • Fighting with the linker • Writing your entire application in a systems language
  3. Garbage Collection class  Point
    def  initialize(x,  y)
    

       @x,  @y  =  x,  y
    end
 end
 
 class  Line
    def  initialize(p1,  p2)
        @p1,  @p2  =  p1,  p2
    end
 
    def  length
        #  compute  length
    end
 end
 def  length(x1,  y1,  x2,  y2)
    p1  =  Point.new(x1,  y1)
    p2  =  Point.new(x2,  y2)
    line  =  Line.new(p1,  p2)
    line.length
 end
  4. Garbage Collection def  length(x1,  y1,  x2,  y2)
    p1  =

     Point.new(x1,  y1)
    p2  =  Point.new(x2,  y2)
    line  =  Line.new(p1,  p2)
    line.length
 end
  5. Garbage Collection def  length(x1,  y1,  x2,  y2)
    p1  =

     Point.new(x1,  y1)
    p2  =  Point.new(x2,  y2)
    line  =  Line.new(p1,  p2)
    line.length
 end
  6. Garbage Collection def  length(x1,  y1,  x2,  y2)
    p1  =

     Point.new(x1,  y1)
    p2  =  Point.new(x2,  y2)
    line  =  Line.new(p1,  p2)
    line.length
 end Point.new(x1,  y1)
  7. Garbage Collection def  length(x1,  y1,  x2,  y2)
    p1  =

     Point.new(x1,  y1)
    p2  =  Point.new(x2,  y2)
    line  =  Line.new(p1,  p2)
    line.length
 end Point.new(x1,  y1)
  8. Garbage Collection def  length(x1,  y1,  x2,  y2)
    p1  =

     Point.new(x1,  y1)
    p2  =  Point.new(x2,  y2)
    line  =  Line.new(p1,  p2)
    line.length
 end Point.new(x1,  y1) Point.new(x2,  y2)
  9. Garbage Collection def  length(x1,  y1,  x2,  y2)
    p1  =

     Point.new(x1,  y1)
    p2  =  Point.new(x2,  y2)
    line  =  Line.new(p1,  p2)
    line.length
 end Point.new(x1,  y1) Point.new(x2,  y2)
  10. Garbage Collection def  length(x1,  y1,  x2,  y2)
    p1  =

     Point.new(x1,  y1)
    p2  =  Point.new(x2,  y2)
    line  =  Line.new(p1,  p2)
    line.length
 end Point.new(x1,  y1) Point.new(x2,  y2) Line.new(p1,  p2)
  11. Garbage Collection def  length(x1,  y1,  x2,  y2)
    p1  =

     Point.new(x1,  y1)
    p2  =  Point.new(x2,  y2)
    line  =  Line.new(p1,  p2)
    line.length
 end Point.new(x1,  y1) Point.new(x2,  y2) Line.new(p1,  p2)
  12. Garbage Collection def  length(x1,  y1,  x2,  y2)
    p1  =

     Point.new(x1,  y1)
    p2  =  Point.new(x2,  y2)
    line  =  Line.new(p1,  p2)
    line.length
 end Point.new(x1,  y1) Point.new(x2,  y2) Line.new(p1,  p2) later… stop the world…
  13. Garbage Collection def  length(x1,  y1,  x2,  y2)
    p1  =

     Point.new(x1,  y1)
    p2  =  Point.new(x2,  y2)
    line  =  Line.new(p1,  p2)
    line.length
 end
  14. Right to Destroy pub  struct  Book  {
    title:  String,

         chapters:  Vec<String>
 }   fn  main()  {      let  book  =  read("book1.txt");      println!("Title:  {}",  book.title);   }
  15. Right to Destroy pub  struct  Book  {
    title:  String,

         chapters:  Vec<String>
 }   fn  main()  {      let  book  =  read("book1.txt");      println!("Title:  {}",  book.title);   }
  16. Right to Destroy pub  struct  Book  {
    title:  String,

         chapters:  Vec<String>
 }   fn  main()  {      let  book  =  read("book1.txt");      println!("Title:  {}",  book.title);   }
  17. Right to Destroy pub  struct  Book  {
    title:  String,

         chapters:  Vec<String>
 }   fn  main()  {      let  book  =  read("book1.txt");      println!("Title:  {}",  book.title);   }
  18. Right to Destroy pub  struct  Book  {
    title:  String,

         chapters:  Vec<String>
 }   fn  main()  {      let  book  =  read("book1.txt");      println!("Title:  {}",  book.title);   }
  19. Right to Destroy pub  struct  Book  {
    title:  String,

         chapters:  Vec<String>
 }   fn  main()  {      let  book  =  read("book1.txt");      println!("Title:  {}",  book.title);   } note the lack of manual memory management
  20. Transferring Ownership pub  struct  Book  {      title:  String,

         chapters:  Vec<String>   }   fn  main()  {      let  book  =  read("book1.txt");      print_book(book);   }   fn  print_book(book:  Book)  {      println!("Title:  {}",  book.title);   }
  21. Transferring Ownership pub  struct  Book  {      title:  String,

         chapters:  Vec<String>   }   fn  main()  {      let  book  =  read("book1.txt");      print_book(book);   }   fn  print_book(book:  Book)  {      println!("Title:  {}",  book.title);   }
  22. Transferring Ownership pub  struct  Book  {      title:  String,

         chapters:  Vec<String>   }   fn  main()  {      let  book  =  read("book1.txt");      print_book(book);   }   fn  print_book(book:  Book)  {      println!("Title:  {}",  book.title);   }
  23. Transferring Ownership pub  struct  Book  {      title:  String,

         chapters:  Vec<String>   }   fn  main()  {      let  book  =  read("book1.txt");      print_book(book);   }   fn  print_book(book:  Book)  {      println!("Title:  {}",  book.title);   }
  24. Transferring Ownership pub  struct  Book  {      title:  String,

         chapters:  Vec<String>   }   fn  main()  {      let  book  =  read("book1.txt");      print_book(book);   }   fn  print_book(book:  Book)  {      println!("Title:  {}",  book.title);   }
  25. Transferring Ownership pub  struct  Book  {      title:  String,

         chapters:  Vec<String>   }   fn  main()  {      let  book  =  read("book1.txt");      print_book(book);   }   fn  print_book(book:  Book)  {      println!("Title:  {}",  book.title);   }
  26. Transferring Ownership pub  struct  Book  {
    title:  String,  

       chapters:  Vec<String>
 }   fn  main()  {      let  book  =  read("book1.txt");      print_book(book);      println!("Chapters:  {}",  book.chapters.len());   }   fn  print_book(book:  Book)  {      println!("Title:  {}",  book.title);   }
  27. Transferring Ownership pub  struct  Book  {
    title:  String,  

       chapters:  Vec<String>
 }   fn  main()  {      let  book  =  read("book1.txt");      print_book(book);      println!("Chapters:  {}",  book.chapters.len());   }   fn  print_book(book:  Book)  {      println!("Title:  {}",  book.title);   }
  28. Transferring Ownership pub  struct  Book  {
    title:  String,  

       chapters:  Vec<String>
 }   fn  main()  {      let  book  =  read("book1.txt");      print_book(book);      println!("Chapters:  {}",  book.chapters.len());   }   fn  print_book(book:  Book)  {      println!("Title:  {}",  book.title);   }
  29. Transferring Ownership pub  struct  Book  {
    title:  String,  

       chapters:  Vec<String>
 }   fn  main()  {      let  book  =  read("book1.txt");      print_book(book);      println!("Chapters:  {}",  book.chapters.len());   }   fn  print_book(book:  Book)  {      println!("Title:  {}",  book.title);   }
  30. Transferring Ownership pub  struct  Book  {
    title:  String,  

       chapters:  Vec<String>
 }   fn  main()  {      let  book  =  read("book1.txt");      print_book(book);      println!("Chapters:  {}",  book.chapters.len());   }   fn  print_book(book:  Book)  {      println!("Title:  {}",  book.title);   }
  31. Transferring Ownership pub  struct  Book  {
    title:  String,  

       chapters:  Vec<String>
 }   fn  main()  {      let  book  =  read("book1.txt");      print_book(book);      println!("Chapters:  {}",  book.chapters.len());   }   fn  print_book(book:  Book)  {      println!("Title:  {}",  book.title);   }
  32. Transferring Ownership pub  struct  Book  {
    title:  String,  

       chapters:  Vec<String>
 }   fn  main()  {      let  book  =  read("book1.txt");      print_book(book);      println!("Chapters:  {}",  book.chapters.len());   }   fn  print_book(book:  Book)  {      println!("Title:  {}",  book.title);   }
  33. Transferring Ownership pub  struct  Book  {
    title:  String,  

       chapters:  Vec<String>
 }   fn  main()  {      let  book  =  read("book1.txt");      print_book(book);      println!("Chapters:  {}",  book.chapters.len());   }   fn  print_book(book:  Book)  {      println!("Title:  {}",  book.title);   } error: use of moved value: `book.chapters` note: `book` moved here
  34. Borrowing pub  struct  Book  {
    title:  String,    

     chapters:  Vec<String>
 }   fn  main()  {      let  book  =  read("book1.txt");      print_book(&book);      println!("Chapters:  {}",  book.chapters.len());   }   fn  print_book(book:  &Book)  {      println!("Title:  {}",  book.title);   }
  35. Borrowing pub  struct  Book  {
    title:  String,    

     chapters:  Vec<String>
 }   fn  main()  {      let  book  =  read("book1.txt");      print_book(&book);      println!("Chapters:  {}",  book.chapters.len());   }   fn  print_book(book:  &Book)  {      println!("Title:  {}",  book.title);   }
  36. Borrowing pub  struct  Book  {
    title:  String,    

     chapters:  Vec<String>
 }   fn  main()  {      let  book  =  read("book1.txt");      print_book(&book);      println!("Chapters:  {}",  book.chapters.len());   }   fn  print_book(book:  &Book)  {      println!("Title:  {}",  book.title);   }
  37. Borrowing pub  struct  Book  {
    title:  String,    

     chapters:  Vec<String>
 }   fn  main()  {      let  book  =  read("book1.txt");      print_book(&book);      println!("Chapters:  {}",  book.chapters.len());   }   fn  print_book(book:  &Book)  {      println!("Title:  {}",  book.title);   }
  38. Borrowing pub  struct  Book  {
    title:  String,    

     chapters:  Vec<String>
 }   fn  main()  {      let  book  =  read("book1.txt");      print_book(&book);      println!("Chapters:  {}",  book.chapters.len());   }   fn  print_book(book:  &Book)  {      println!("Title:  {}",  book.title);   }
  39. Borrowing pub  struct  Book  {
    title:  String,    

     chapters:  Vec<String>
 }   fn  main()  {      let  book  =  read("book1.txt");      print_book(&book);      println!("Chapters:  {}",  book.chapters.len());   }   fn  print_book(book:  &Book)  {      println!("Title:  {}",  book.title);   }
  40. Borrowing pub  struct  Book  {
    title:  String,    

     chapters:  Vec<String>
 }   fn  main()  {      let  book  =  read("book1.txt");      print_book(&book);      println!("Chapters:  {}",  book.chapters.len());   }   fn  print_book(book:  &Book)  {      println!("Title:  {}",  book.title);   }
  41. Borrowing pub  struct  Book  {
    title:  String,    

     chapters:  Vec<String>
 }   fn  main()  {      let  book  =  read("book1.txt");      print_book(&book);      println!("Chapters:  {}",  book.chapters.len());   }   fn  print_book(book:  &Book)  {      println!("Title:  {}",  book.title);   }
  42. Sub-Leasing fn  main()  {      let  book  =  read_book("book1.txt");

         print_book(&book);   }   pub  struct  Book  {
    title:  String,      chapters:  Vec<String>
 }   fn  print_book(book:  &Book)  {      print_title(&book);      print_chapters(&book);   }   fn  print_title(book:  &Book)  {      println!("Title:  {}",  book.title);   }   fn  print_chapters(book:  &Book)  {      let  len  =  book.chapters.len();      println!("Chapters:  {}",  len);   }
  43. Sub-Leasing fn  main()  {      let  book  =  read_book("book1.txt");

         print_book(&book);   }   pub  struct  Book  {
    title:  String,      chapters:  Vec<String>
 }   fn  print_book(book:  &Book)  {      print_title(&book);      print_chapters(&book);   }   fn  print_title(book:  &Book)  {      println!("Title:  {}",  book.title);   }   fn  print_chapters(book:  &Book)  {      let  len  =  book.chapters.len();      println!("Chapters:  {}",  len);   }
  44. Sub-Leasing fn  main()  {      let  book  =  read_book("book1.txt");

         print_book(&book);   }   pub  struct  Book  {
    title:  String,      chapters:  Vec<String>
 }   fn  print_book(book:  &Book)  {      print_title(&book);      print_chapters(&book);   }   fn  print_title(book:  &Book)  {      println!("Title:  {}",  book.title);   }   fn  print_chapters(book:  &Book)  {      let  len  =  book.chapters.len();      println!("Chapters:  {}",  len);   }
  45. Sub-Leasing fn  main()  {      let  book  =  read_book("book1.txt");

         print_book(&book);   }   pub  struct  Book  {
    title:  String,      chapters:  Vec<String>
 }   fn  print_book(book:  &Book)  {      print_title(&book);      print_chapters(&book);   }   fn  print_title(book:  &Book)  {      println!("Title:  {}",  book.title);   }   fn  print_chapters(book:  &Book)  {      let  len  =  book.chapters.len();      println!("Chapters:  {}",  len);   }
  46. Sub-Leasing fn  main()  {      let  book  =  read_book("book1.txt");

         print_book(&book);   }   pub  struct  Book  {
    title:  String,      chapters:  Vec<String>
 }   fn  print_book(book:  &Book)  {      print_title(&book);      print_chapters(&book);   }   fn  print_title(book:  &Book)  {      println!("Title:  {}",  book.title);   }   fn  print_chapters(book:  &Book)  {      let  len  =  book.chapters.len();      println!("Chapters:  {}",  len);   }
  47. Sub-Leasing fn  main()  {      let  book  =  read_book("book1.txt");

         print_book(&book);   }   pub  struct  Book  {
    title:  String,      chapters:  Vec<String>
 }   fn  print_book(book:  &Book)  {      print_title(&book);      print_chapters(&book);   }   fn  print_title(book:  &Book)  {      println!("Title:  {}",  book.title);   }   fn  print_chapters(book:  &Book)  {      let  len  =  book.chapters.len();      println!("Chapters:  {}",  len);   }
  48. Sub-Leasing fn  main()  {      let  book  =  read_book("book1.txt");

         print_book(&book);   }   pub  struct  Book  {
    title:  String,      chapters:  Vec<String>
 }   fn  print_book(book:  &Book)  {      print_title(&book);      print_chapters(&book);   }   fn  print_title(book:  &Book)  {      println!("Title:  {}",  book.title);   }   fn  print_chapters(book:  &Book)  {      let  len  =  book.chapters.len();      println!("Chapters:  {}",  len);   }
  49. Sub-Leasing fn  main()  {      let  book  =  read_book("book1.txt");

         print_book(&book);   }   pub  struct  Book  {
    title:  String,      chapters:  Vec<String>
 }   fn  print_book(book:  &Book)  {      print_title(&book);      print_chapters(&book);   }   fn  print_title(book:  &Book)  {      println!("Title:  {}",  book.title);   }   fn  print_chapters(book:  &Book)  {      let  len  =  book.chapters.len();      println!("Chapters:  {}",  len);   }
  50. Sub-Leasing fn  main()  {      let  book  =  read_book("book1.txt");

         print_book(&book);   }   pub  struct  Book  {
    title:  String,      chapters:  Vec<String>
 }   fn  print_book(book:  &Book)  {      print_title(&book);      print_chapters(&book);   }   fn  print_title(book:  &Book)  {      println!("Title:  {}",  book.title);   }   fn  print_chapters(book:  &Book)  {      let  len  =  book.chapters.len();      println!("Chapters:  {}",  len);   }
  51. Sub-Leasing fn  main()  {      let  book  =  read_book("book1.txt");

         print_book(&book);   }   pub  struct  Book  {
    title:  String,      chapters:  Vec<String>
 }   fn  print_book(book:  &Book)  {      print_title(&book);      print_chapters(&book);   }   fn  print_title(book:  &Book)  {      println!("Title:  {}",  book.title);   }   fn  print_chapters(book:  &Book)  {      let  len  =  book.chapters.len();      println!("Chapters:  {}",  len);   }
  52. Sub-Leasing fn  main()  {      let  book  =  read_book("book1.txt");

         print_book(&book);   }   pub  struct  Book  {
    title:  String,      chapters:  Vec<String>
 }   fn  print_book(book:  &Book)  {      print_title(&book);      print_chapters(&book);   }   fn  print_title(book:  &Book)  {      println!("Title:  {}",  book.title);   }   fn  print_chapters(book:  &Book)  {      let  len  =  book.chapters.len();      println!("Chapters:  {}",  len);   }
  53. Sub-Leasing fn  main()  {      let  book  =  read_book("book1.txt");

         print_book(&book);   }   pub  struct  Book  {
    title:  String,      chapters:  Vec<String>
 }   fn  print_book(book:  &Book)  {      print_title(&book);      print_chapters(&book);   }   fn  print_title(book:  &Book)  {      println!("Title:  {}",  book.title);   }   fn  print_chapters(book:  &Book)  {      let  len  =  book.chapters.len();      println!("Chapters:  {}",  len);   }
  54. Sub-Leasing fn  main()  {      let  book  =  read_book("book1.txt");

         print_book(&book);   }   pub  struct  Book  {
    title:  String,      chapters:  Vec<String>
 }   fn  print_book(book:  &Book)  {      print_title(&book);      print_chapters(&book);   }   fn  print_title(book:  &Book)  {      println!("Title:  {}",  book.title);   }   fn  print_chapters(book:  &Book)  {      let  len  =  book.chapters.len();      println!("Chapters:  {}",  len);   }
  55. Sub-Leasing fn  main()  {      let  book  =  read_book("book1.txt");

         print_book(&book);   }   pub  struct  Book  {
    title:  String,      chapters:  Vec<String>
 }   fn  print_book(book:  &Book)  {      print_title(&book);      print_chapters(&book);   }   fn  print_title(book:  &Book)  {      println!("Title:  {}",  book.title);   }   fn  print_chapters(book:  &Book)  {      let  len  =  book.chapters.len();      println!("Chapters:  {}",  len);   }
  56. fn  main()  {      let  mut  book  =  read_book("book1.txt");

         print_book(&mut  book);   }   pub  struct  Book  {
    title:  String,      chapters:  Vec<String>,      bookmark:  u32
 }   fn  print_book(book:  &mut  Book)  {      print_title(&book);      print_chapters(&book);      book.bookmark  =  100;   }   fn  print_title(book:  &Book)  {      println!("Title:  {}",  book.title);   }   fn  print_chapters(book:  &Book)  {      let  len  =  book.chapters.len();      println!("Chapters:  {}",  len);   } Sub-Leasing
  57. fn  main()  {      let  mut  book  =  read_book("book1.txt");

         print_book(&mut  book);   }   pub  struct  Book  {
    title:  String,      chapters:  Vec<String>,      bookmark:  u32
 }   fn  print_book(book:  &mut  Book)  {      print_title(&book);      print_chapters(&book);      book.bookmark  =  100;   }   fn  print_title(book:  &Book)  {      println!("Title:  {}",  book.title);   }   fn  print_chapters(book:  &Book)  {      let  len  =  book.chapters.len();      println!("Chapters:  {}",  len);   } Sub-Leasing
  58. fn  main()  {      let  mut  book  =  read_book("book1.txt");

         print_book(&mut  book);   }   pub  struct  Book  {
    title:  String,      chapters:  Vec<String>,      bookmark:  u32
 }   fn  print_book(book:  &mut  Book)  {      print_title(&book);      print_chapters(&book);      book.bookmark  =  100;   }   fn  print_title(book:  &Book)  {      println!("Title:  {}",  book.title);   }   fn  print_chapters(book:  &Book)  {      let  len  =  book.chapters.len();      println!("Chapters:  {}",  len);   } Sub-Leasing
  59. fn  main()  {      let  mut  book  =  read_book("book1.txt");

         print_book(&mut  book);   }   pub  struct  Book  {
    title:  String,      chapters:  Vec<String>,      bookmark:  u32
 }   fn  print_book(book:  &mut  Book)  {      print_title(&book);      print_chapters(&book);      book.bookmark  =  100;   }   fn  print_title(book:  &Book)  {      println!("Title:  {}",  book.title);   }   fn  print_chapters(book:  &Book)  {      let  len  =  book.chapters.len();      println!("Chapters:  {}",  len);   } Sub-Leasing
  60. fn  main()  {      let  mut  book  =  read_book("book1.txt");

         print_book(&mut  book);   }   pub  struct  Book  {
    title:  String,      chapters:  Vec<String>,      bookmark:  u32
 }   fn  print_book(book:  &mut  Book)  {      print_title(&book);      print_chapters(&book);      book.bookmark  =  100;   }   fn  print_title(book:  &Book)  {      println!("Title:  {}",  book.title);   }   fn  print_chapters(book:  &Book)  {      let  len  =  book.chapters.len();      println!("Chapters:  {}",  len);   } Sub-Leasing
  61. fn  main()  {      let  mut  book  =  read_book("book1.txt");

         print_book(&mut  book);   }   pub  struct  Book  {
    title:  String,      chapters:  Vec<String>,      bookmark:  u32
 }   fn  print_book(book:  &mut  Book)  {      print_title(&book);      print_chapters(&book);      book.bookmark  =  100;   }   fn  print_title(book:  &Book)  {      println!("Title:  {}",  book.title);   }   fn  print_chapters(book:  &Book)  {      let  len  =  book.chapters.len();      println!("Chapters:  {}",  len);   } Sub-Leasing
  62. fn  main()  {      let  mut  book  =  read_book("book1.txt");

         print_book(&mut  book);   }   pub  struct  Book  {
    title:  String,      chapters:  Vec<String>,      bookmark:  u32
 }   fn  print_book(book:  &mut  Book)  {      print_title(&book);      print_chapters(&book);      book.bookmark  =  100;   }   fn  print_title(book:  &Book)  {      println!("Title:  {}",  book.title);   }   fn  print_chapters(book:  &Book)  {      let  len  =  book.chapters.len();      println!("Chapters:  {}",  len);   } Sub-Leasing
  63. fn  main()  {      let  mut  book  =  read_book("book1.txt");

         print_book(&mut  book);   }   pub  struct  Book  {
    title:  String,      chapters:  Vec<String>,      bookmark:  u32
 }   fn  print_book(book:  &mut  Book)  {      print_title(&book);      print_chapters(&book);      book.bookmark  =  100;   }   fn  print_title(book:  &Book)  {      println!("Title:  {}",  book.title);   }   fn  print_chapters(book:  &Book)  {      let  len  =  book.chapters.len();      println!("Chapters:  {}",  len);   } Sub-Leasing
  64. fn  main()  {      let  mut  book  =  read_book("book1.txt");

         print_book(&mut  book);   }   pub  struct  Book  {
    title:  String,      chapters:  Vec<String>,      bookmark:  u32
 }   fn  print_book(book:  &mut  Book)  {      print_title(&book);      print_chapters(&book);      book.bookmark  =  100;   }   fn  print_title(book:  &Book)  {      println!("Title:  {}",  book.title);   }   fn  print_chapters(book:  &Book)  {      let  len  =  book.chapters.len();      println!("Chapters:  {}",  len);   } Sub-Leasing
  65. fn  main()  {      let  mut  book  =  read_book("book1.txt");

         print_book(&mut  book);   }   pub  struct  Book  {
    title:  String,      chapters:  Vec<String>,      bookmark:  u32
 }   fn  print_book(book:  &mut  Book)  {      print_title(&book);      print_chapters(&book);      book.bookmark  =  100;   }   fn  print_title(book:  &Book)  {      println!("Title:  {}",  book.title);   }   fn  print_chapters(book:  &Book)  {      let  len  =  book.chapters.len();      println!("Chapters:  {}",  len);   } Sub-Leasing
  66. fn  main()  {      let  mut  book  =  read_book("book1.txt");

         print_book(&mut  book);   }   pub  struct  Book  {
    title:  String,      chapters:  Vec<String>,      bookmark:  u32
 }   fn  print_book(book:  &mut  Book)  {      print_title(&book);      print_chapters(&book);      book.bookmark  =  100;   }   fn  print_title(book:  &Book)  {      println!("Title:  {}",  book.title);   }   fn  print_chapters(book:  &Book)  {      let  len  =  book.chapters.len();      println!("Chapters:  {}",  len);   } Sub-Leasing
  67. fn  main()  {      let  mut  book  =  read_book("book1.txt");

         print_book(&mut  book);   }   pub  struct  Book  {
    title:  String,      chapters:  Vec<String>,      bookmark:  u32
 }   fn  print_book(book:  &mut  Book)  {      print_title(&book);      print_chapters(&book);      book.bookmark  =  100;   }   fn  print_title(book:  &Book)  {      println!("Title:  {}",  book.title);   }   fn  print_chapters(book:  &Book)  {      let  len  =  book.chapters.len();      println!("Chapters:  {}",  len);   } Sub-Leasing
  68. fn  main()  {      let  mut  book  =  read_book("book1.txt");

         print_book(&mut  book);   }   pub  struct  Book  {
    title:  String,      chapters:  Vec<String>,      bookmark:  u32
 }   fn  print_book(book:  &mut  Book)  {      print_title(&book);      print_chapters(&book);      book.bookmark  =  100;   }   fn  print_title(book:  &Book)  {      println!("Title:  {}",  book.title);   }   fn  print_chapters(book:  &Book)  {      let  len  =  book.chapters.len();      println!("Chapters:  {}",  len);   } Sub-Leasing
  69. fn  main()  {      let  mut  book  =  read_book("book1.txt");

         print_book(&mut  book);   }   pub  struct  Book  {
    title:  String,      chapters:  Vec<String>,      bookmark:  u32
 }   fn  print_book(book:  &mut  Book)  {      print_title(&book);      print_chapters(&book);      book.bookmark  =  100;   }   fn  print_title(book:  &Book)  {      println!("Title:  {}",  book.title);   }   fn  print_chapters(book:  &Book)  {      let  len  =  book.chapters.len();      println!("Chapters:  {}",  len);   } Sub-Leasing
  70. fn  main()  {      let  mut  book  =  read_book("book1.txt");

         print_book(&mut  book);   }   pub  struct  Book  {
    title:  String,      chapters:  Vec<String>,      bookmark:  u32
 }   fn  print_book(book:  &mut  Book)  {      print_title(&book);      print_chapters(&book);      book.bookmark  =  100;   }   fn  print_title(book:  &Book)  {      println!("Title:  {}",  book.title);   }   fn  print_chapters(book:  &Book)  {      let  len  =  book.chapters.len();      println!("Chapters:  {}",  len);   } Sub-Leasing
  71. Rules of Borrowing • You can have many outstanding read-only

    borrows at once • A mutable borrow is unique: no other borrows, mutable
 or read-only, can occur at the same time
  72. Rules of Borrowing • You can have many outstanding read-only

    borrows at once • A mutable borrow is unique: no other borrows, mutable
 or read-only, can occur at the same time • These rules are enforced by the compiler, not at runtime
  73. Read-Only Borrows fn  main()  {      let  book1  =

     read_book("book1.txt");      let  book2  =  read_book("book2.txt");      same(&book1,  &book2);   }   pub  struct  Book  {
    title:  String,      chapters:  Vec<String>   }   fn  same(a:  &Book,  b:  &Book)  -­‐>  bool  {      a.title  ==  b.title  &&      a.chapters  ==  b.chapters   }
  74. Read-Only Borrows fn  main()  {      let  book1  =

     read_book("book1.txt");      let  book2  =  read_book("book2.txt");      same(&book1,  &book2);   }   pub  struct  Book  {
    title:  String,      chapters:  Vec<String>   }   fn  same(a:  &Book,  b:  &Book)  -­‐>  bool  {      a.title  ==  b.title  &&      a.chapters  ==  b.chapters   } ✅
  75. Two Read-Only Borrows fn  main()  {      let  book1

     =  read_book("book1.txt");      same(&book1,  &book1);   }   pub  struct  Book  {
    title:  String,      chapters:  Vec<String>   }   fn  same(a:  &Book,  b:  &Book)  -­‐>  bool  {      a.title  ==  b.title  &&      a.chapters  ==  b.chapters   }
  76. Two Read-Only Borrows fn  main()  {      let  book1

     =  read_book("book1.txt");      same(&book1,  &book1);   }   pub  struct  Book  {
    title:  String,      chapters:  Vec<String>   }   fn  same(a:  &Book,  b:  &Book)  -­‐>  bool  {      a.title  ==  b.title  &&      a.chapters  ==  b.chapters   } ✅
  77. Mutable Borrows Must Be Unique pub  struct  Book  {  

       title:  String,      chapters:  Vec<String>   }   fn  main()  {      let  book1  =  read_book("book1.txt");      let  mut  book2  =  read_book("book2.txt");      copy(&book1,  &mut  book2);   }   fn  copy(a:  &Book,  b:  &mut  Book)  {      b.title  =  a.title;   }
  78. Mutable Borrows Must Be Unique pub  struct  Book  {  

       title:  String,      chapters:  Vec<String>   }   fn  main()  {      let  book1  =  read_book("book1.txt");      let  mut  book2  =  read_book("book2.txt");      copy(&book1,  &mut  book2);   }   fn  copy(a:  &Book,  b:  &mut  Book)  {      b.title  =  a.title;   } ✅
  79. pub  struct  Book  {      title:  String,    

     chapters:  Vec<String>   }   fn  main()  {      let  mut  book  =  read_book("book1.txt");      copy(&book,  &mut  book);   }   fn  copy(a:  &Book,  b:  &mut  Book)  {      b.title  =  a.title;   } Mutable Borrows Must Be Unique
  80. pub  struct  Book  {      title:  String,    

     chapters:  Vec<String>   }   fn  main()  {      let  mut  book  =  read_book("book1.txt");      copy(&book,  &mut  book);   }   fn  copy(a:  &Book,  b:  &mut  Book)  {      b.title  =  a.title;   } Mutable Borrows Must Be Unique
  81. pub  struct  Book  {      title:  String,    

     chapters:  Vec<String>   }   fn  main()  {      let  mut  book  =  read_book("book1.txt");      copy(&book,  &mut  book);   }   fn  copy(a:  &Book,  b:  &mut  Book)  {      b.title  =  a.title;   } Mutable Borrows Must Be Unique ^~~~ error: cannot borrow `book` as mutable because it is also borrowed as immutable
  82. Mutable Borrows Must Be Unique pub  struct  Book  {  

       title:  String,      chapters:  Vec<String>   }   fn  main()  {      let  mut  book  =  read_book("book1.txt");      copy(&book,  &mut  book);   }   fn  copy(a:  &Book,  b:  &mut  Book)  {      b.title  =  a.title;   }
  83. Mutable Borrows Must Be Unique pub  struct  Book  {  

       title:  String,      chapters:  Vec<String>   }   fn  main()  {      let  mut  book  =  read_book("book1.txt");      copy(&book,  &mut  book);   }   fn  copy(a:  &Book,  b:  &mut  Book)  {      b.title  =  a.title;   } ^~~~ note: previous borrow of `book` occurs here
  84. Mutable Borrows Must Be Unique pub  struct  Book  {  

       title:  String,      chapters:  Vec<String>   }   fn  main()  {      let  mut  book  =  read_book("book1.txt");      copy(&book,  &mut  book);   }   fn  copy(a:  &Book,  b:  &mut  Book)  {      b.title  =  a.title;   }
  85. Mutable Borrows Must Be Unique pub  struct  Book  {  

       title:  String,      chapters:  Vec<String>   }   fn  main()  {      let  mut  book  =  read_book("book1.txt");      copy(&book,  &mut  book);   }   fn  copy(a:  &Book,  b:  &mut  Book)  {      b.title  =  a.title;   } ^ note: previous borrow ends here
  86. Closures fn  main()  {      println!("{:?}",  threes_squared(10));   }

      fn  threes_squared(upto:  u32)  -­‐>  Vec<u32>  {      (0..upto)          .filter(|i|  i  %  3  ==  0)          .map(|i|  i.pow(2))          .collect()   }
  87. Closed Over Variables fn  main()  {      println!("{:?}",  threes_squared(10,

     "LOG"));   }   fn  threes_squared(upto:  u32,  string:  &str)  -­‐>  Vec<String>  {      (0..upto)          .filter(|i|  i  %  3  ==  0)          .map(|i|  format!("{}:  {}",  string,  i.pow(2)))          .collect()   }
  88. Equivalent Performance fn  main()  {      println!("{:?}",  threes_squared(10,  "LOG"));

      }   fn  threes_squared(upto:  u32,  string:  &str)  -­‐>  Vec<String>  {
    let  v  =  vec![];      loop  {
        if  i  %  3  ==  0  {              v.push(format!("{}:  {}",  string,  i  *  i));          }          if  i  <  upto  {  break;  }
    }      v   }
  89. Details Abstracted fn  main()  {      println!("{:?}",  threes_squared(10,  "LOG"));

      }   fn  threes_squared(upto:  u32,  string:  &str)  -­‐>  Vec<String>  {
    let  v  =  Vec::with_capacity(upto  -­‐  1);      loop  {
        if  i  %  3  ==  0  {              v.push(format!("{}:  {}",  string,  i  *  i));          }          if  i  <  upto  {  break;  }
    }      v   }
  90. Details Abstracted fn  main()  {      println!("{:?}",  threes_squared(10,  "LOG"));

      }   fn  threes_squared(upto:  u32,  string:  &str)  -­‐>  Vec<String>  {
    let  v  =  Vec::with_capacity(upto  -­‐  1);      loop  {
        if  i  %  3  ==  0  {              v.push(format!("{}:  {}",  string,  i  *  i));          }          if  i  <  upto  {  break;  }
    }      v   } Bounds Checks
  91. FnOnce A closure that only runs once and can take

    ownership from its environment Fn A closure that cannot mutate its environment and can be shared across threads FnMut A closure that can mutate its environment but cannot be shared across threads
  92. FnOnce fn  main()  {      let  buf  =  String::new();

         stdin().read_line(&mut  buf).map(|size|  {
        println!("Read  {}  bytes",  size);          transfer(buf);
    });   }   fn  transfer(string:  String)  {      println!("Got  transferred  {:?}",  string);
 }
  93. Rules of Borrowing • You can have many outstanding read-only

    borrows at once • A mutable borrow is unique: no other borrows, mutable
 or read-only, can occur at the same time
  94. Rules of Borrowing • You can have many outstanding read-only

    borrows at once • A mutable borrow is unique: no other borrows, mutable
 or read-only, can occur at the same time • You can alias or mutate, but not both at the same time
  95. Send Safe to transfer across threads (no borrowed values) Sync

    Safe to share across threads (no racy "interior mutability")
  96. spawn() fn  main()  {      let  book  =  read_book("book1.txt");

         spawn(||  {          println!("{}",  book);      });   }
  97. spawn() fn  main()  {      let  book  =  read_book("book1.txt");

         spawn(||  {          println!("{}",  book);      });   }
  98. spawn() fn  main()  {      let  book  =  read_book("book1.txt");

         spawn(||  {          println!("{}",  book);      });   } ^~~~ error: `book` does not live long enough
  99. spawn() fn  main()  {      let  book  =  read_book("book1.txt");

         spawn(move  ||  {          println!("{}",  book);      });   }
  100. scoped() fn  main()  {      let  book  =  read_book("book1.txt");

         let  guard  =  scoped(||  {          println!("{}",  book);      });
 
    //  do  other  work
 
    let  output  =  guard.join();   }
  101. scoped() fn  main()  {      let  book  =  read_book("book1.txt");

         let  guard  =  scoped(||  {          println!("{}",  book);      });
 
    //  do  other  work
 
    let  output  =  guard.join();  //  default  impl  of  Drop   }
  102. OO

  103. OO Syntax struct  Circle  {      x:  f64,  

       y:  f64,      radius:  f64,   }   impl  Circle  {      fn  area(&self)  -­‐>  f64  {          std::f64::consts::PI  *  (self.radius  *  self.radius)      }   }