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.

428167a3ec72235ba971162924492609?s=128

Yehuda Katz

April 07, 2015
Tweet

Transcript

  1. 1.
  2. 2.
  3. 16.

    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
  4. 17.

    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
  5. 18.
  6. 20.
  7. 23.
  8. 24.

    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
  9. 25.

    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
  10. 26.

    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
  11. 27.

    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)
  12. 28.

    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)
  13. 29.

    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)
  14. 30.

    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)
  15. 31.

    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)
  16. 32.

    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)
  17. 33.

    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…
  18. 34.

    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
  19. 35.
  20. 36.
  21. 37.
  22. 38.
  23. 39.
  24. 40.
  25. 41.
  26. 42.
  27. 44.

    Right to Destroy pub  struct  Book  {
    title:  String,

         chapters:  Vec<String>
 }   fn  main()  {      let  book  =  read("book1.txt");      println!("Title:  {}",  book.title);   }
  28. 45.

    Right to Destroy pub  struct  Book  {
    title:  String,

         chapters:  Vec<String>
 }   fn  main()  {      let  book  =  read("book1.txt");      println!("Title:  {}",  book.title);   }
  29. 46.

    Right to Destroy pub  struct  Book  {
    title:  String,

         chapters:  Vec<String>
 }   fn  main()  {      let  book  =  read("book1.txt");      println!("Title:  {}",  book.title);   }
  30. 47.

    Right to Destroy pub  struct  Book  {
    title:  String,

         chapters:  Vec<String>
 }   fn  main()  {      let  book  =  read("book1.txt");      println!("Title:  {}",  book.title);   }
  31. 48.

    Right to Destroy pub  struct  Book  {
    title:  String,

         chapters:  Vec<String>
 }   fn  main()  {      let  book  =  read("book1.txt");      println!("Title:  {}",  book.title);   }
  32. 49.

    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
  33. 50.

    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);   }
  34. 51.

    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);   }
  35. 52.

    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);   }
  36. 53.

    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);   }
  37. 54.

    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);   }
  38. 55.

    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);   }
  39. 56.

    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);   }
  40. 57.

    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);   }
  41. 58.

    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);   }
  42. 59.

    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);   }
  43. 60.

    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);   }
  44. 61.

    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);   }
  45. 62.

    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);   }
  46. 63.

    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
  47. 69.

    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);   }
  48. 70.

    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);   }
  49. 71.

    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);   }
  50. 72.

    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);   }
  51. 73.

    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);   }
  52. 74.

    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);   }
  53. 75.

    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);   }
  54. 76.

    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);   }
  55. 78.
  56. 79.
  57. 83.
  58. 84.
  59. 85.
  60. 86.

    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);   }
  61. 87.

    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);   }
  62. 88.

    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);   }
  63. 89.

    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);   }
  64. 90.

    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);   }
  65. 91.

    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);   }
  66. 92.

    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);   }
  67. 93.

    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);   }
  68. 94.

    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);   }
  69. 95.

    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);   }
  70. 96.

    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);   }
  71. 97.

    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);   }
  72. 98.

    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);   }
  73. 99.

    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);   }
  74. 100.
  75. 103.
  76. 104.
  77. 105.
  78. 106.

    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
  79. 107.

    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
  80. 108.

    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
  81. 109.

    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
  82. 110.

    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
  83. 111.

    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
  84. 112.

    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
  85. 113.

    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
  86. 114.

    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
  87. 115.

    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
  88. 116.

    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
  89. 117.

    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
  90. 118.

    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
  91. 119.

    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
  92. 120.

    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
  93. 121.

    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. 122.

    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
  95. 123.

    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   }
  96. 124.

    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   } ✅
  97. 125.

    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   }
  98. 126.

    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   } ✅
  99. 127.

    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;   }
  100. 128.

    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;   } ✅
  101. 129.

    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
  102. 130.

    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
  103. 131.

    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
  104. 132.

    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;   }
  105. 133.

    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
  106. 134.

    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;   }
  107. 135.

    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
  108. 137.
  109. 138.

    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()   }
  110. 139.

    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()   }
  111. 140.

    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   }
  112. 141.

    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   }
  113. 142.

    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
  114. 144.

    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
  115. 145.

    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);
 }
  116. 149.
  117. 153.

    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
  118. 154.

    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
  119. 155.

    Send Safe to transfer across threads (no borrowed values) Sync

    Safe to share across threads (no racy "interior mutability")
  120. 156.

    spawn() fn  main()  {      let  book  =  read_book("book1.txt");

         spawn(||  {          println!("{}",  book);      });   }
  121. 157.

    spawn() fn  main()  {      let  book  =  read_book("book1.txt");

         spawn(||  {          println!("{}",  book);      });   }
  122. 158.

    spawn() fn  main()  {      let  book  =  read_book("book1.txt");

         spawn(||  {          println!("{}",  book);      });   } ^~~~ error: `book` does not live long enough
  123. 159.

    spawn() fn  main()  {      let  book  =  read_book("book1.txt");

         spawn(move  ||  {          println!("{}",  book);      });   }
  124. 160.

    scoped() fn  main()  {      let  book  =  read_book("book1.txt");

         let  guard  =  scoped(||  {          println!("{}",  book);      });
 
    //  do  other  work
 
    let  output  =  guard.join();   }
  125. 161.

    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   }
  126. 163.

    OO

  127. 164.

    OO Syntax struct  Circle  {      x:  f64,  

       y:  f64,      radius:  f64,   }   impl  Circle  {      fn  area(&self)  -­‐>  f64  {          std::f64::consts::PI  *  (self.radius  *  self.radius)      }   }
  128. 165.
  129. 166.
  130. 168.
  131. 169.