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.

428167a3ec72235ba971162924492609?s=128

Yehuda Katz

April 07, 2015
Tweet

Transcript

  1. None
  2. Node.js

  3. Expressiveness Speed JS

  4. Underestimated: Ubiquity

  5. Client Side Server Side JS JS

  6. Expressiveness Speed JS

  7. Expressiveness Speed JS V8

  8. Expressiveness Speed JS V8 ES6+

  9. Enabling Technologies

  10. Me, A Systems Programmer?

  11. Me, A Systems Programmer? That's someone else's job…

  12. Me, A Systems Programmer?

  13. Me, A Systems Programmer? "Systems programming" sounds hard… and dangerous

  14. Me, A Systems Programmer?

  15. Me, A Systems Programmer? What is "Systems programming" anyway?

  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
  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
  18. None
  19. Embedding, Performance, Low Memory

  20. None
  21. Fast, Safe, Parallel

  22. How Does it Work?

  23. None
  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
  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
  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
  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)
  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)
  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)
  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)
  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)
  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)
  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…
  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
  35. Ownership

  36. None
  37. None
  38. None
  39. None
  40. None
  41. None
  42. None
  43. Ownership is the Right to Destroy

  44. Right to Destroy pub  struct  Book  {
    title:  String,

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

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

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

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

         chapters:  Vec<String>
 }   fn  main()  {      let  book  =  read("book1.txt");      println!("Title:  {}",  book.title);   }
  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
  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);   }
  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);   }
  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);   }
  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);   }
  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);   }
  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);   }
  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);   }
  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);   }
  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);   }
  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);   }
  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);   }
  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);   }
  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);   }
  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
  64. Borrowing v. To receive something with the promise of returning

    it
  65. Return this by 5pm on Friday

  66. Return this by 5pm on Friday

  67. Return this by 5pm on Friday

  68. Return this by 5pm on Friday

  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);   }
  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);   }
  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);   }
  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);   }
  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);   }
  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);   }
  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);   }
  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);   }
  77. Sub-Leasing

  78. None
  79. None
  80. Return this by 5pm on Friday

  81. Return this by 5pm on Friday

  82. Return this by 4:30pm on Friday

  83. None
  84. None
  85. None
  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);   }
  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);   }
  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);   }
  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);   }
  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);   }
  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);   }
  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);   }
  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);   }
  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);   }
  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);   }
  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);   }
  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);   }
  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);   }
  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);   }
  100. ✏️

  101. Return this by 5pm on Friday and feel free to

    change it ✏️
  102. Return this by 4:30pm on Friday but don't change it

    ✏️
  103. ✏️

  104. ✏️

  105. None
  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
  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
  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
  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
  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
  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
  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
  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
  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
  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
  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
  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
  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
  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
  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
  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
  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
  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   }
  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   } ✅
  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   }
  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   } ✅
  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;   }
  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;   } ✅
  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
  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
  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
  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;   }
  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
  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;   }
  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
  136. Borrowing is the Secret Sauce

  137. Closures

  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()   }
  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()   }
  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   }
  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   }
  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
  143. Same Ownership Rules

  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
  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);
 }
  146. Unified Calling Syntax

  147. Concurrency

  148. "Shared Mutable State is the Root of All Evil"

  149. None
  150. Channels Forbid Aliasing

  151. Channels Forbid Aliasing Functional Forbid Mutation

  152. Channels Forbid Aliasing Functional Forbid Mutation Rust Forbid Both at

    the Same Time
  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
  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
  155. Send Safe to transfer across threads (no borrowed values) Sync

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

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

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

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

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

         let  guard  =  scoped(||  {          println!("{}",  book);      });
 
    //  do  other  work
 
    let  output  =  guard.join();   }
  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   }
  162. High Level Productivity

  163. OO

  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)      }   }
  165. Traits

  166. Iterators

  167. Enums with Methods

  168. Operators

  169. Thank You!

  170. Questions? @wycats