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

Introduction to Rust

Introduction to Rust

Apresentação realizada no evento Colóquios de Ciência da Computação da galera da Universidade do Estado de Santa Catarina.


Alan R. Fachini

July 14, 2021


  1. Introduction to Rust own your garbage, so others don't have

    to collect it for you Colóquios Ciência da Computação 14/07/2021
  2. Alan R. Fachini alfakini alanfachini alfakini.com

  3. Pq eu fui aprender Rust?

  4. None
  5. Por que você deveria aprender Rust? • Não existe um

    grande mercado 😭 • Comunidade legal • Muito material disponível • Oportunidade de trabalhar com memory management, borrow checker, lifetimes, tipos expressivos • Aprender boas práticas de desenvolvimento com mensagens de erro • Not Haskell, but functional Programming and types • Not List, but Macros
  6. None
  7. Introduction

  8. Rust A system programming language focused on three goal: safety,

    speed, and concurrency.
  9. Why Rust? • Desempenho like C/C++ • System Programming Language

    • Guaranteed memory safe, no memory leak • Threads without data races • No runtime, no Garbage Collection • No undefined behaviour • Zero-cost abstractions • Ergonômica, developer happyness • Expressive data structures • Pattern matching • Type inference
  10. Are we * yet?

  11. Are we * yet? • Firefox (Servo) • Linux •

    Railcae (container engine Oracle) • Embedded devices: ARM, Intel, Microsoft Azure IoT Edge • Web: OpenDNS, Discord, Facebook ... • Tor • WebAssembly wasm web standard • Command-line apps • Network services
  12. Comparada ao C, Rust é • Menos verbosa • Não

    é baseada em gambiarra • Fácil de escrever testes automatizados • Fácil de gerir dependências • Fácil de gerir projetos • De modo geral, Integração Contínua é muito mais fácil
  13. Tools • rustup: the rust toolchain installer • rustc: the

    rust compiler • cargo: the package and project manager • rustdoc: the documentation builder
  14. curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

  15. $ cargo new hello_cargo $ cd hello_cargo

  16. fn main() { println!("Hello, world!"); }

  17. $ cargo build Compiling hello_cargo v0.1.0 (file:///projects/hello_cargo) Finished dev [unoptimized

    + debuginfo] target(s) in 2.85 secs
  18. $ cargo run Finished dev [unoptimized + debuginfo] target(s) in

    0.0 secs Running `target/debug/hello_cargo` Hello, world!
  19. Release flow • RFCs: https://github.com/rust-lang/rfcs • Nightly: include unstable features

    • Beta: upcoming stable releases • Stable: new features with docs every 6 weeks
  20. Ecossistema • crates.io: registry for libs and applications • docs.rs:

    documentation for published libs • https://users.rust-lang.org • https://discordapp.com/invite/rust-lang • https://www.rust-lang.org/community
  21. Ferries, the crab crustacean

  22. Everything is about control and safety

  23. Mais controle, mais segurança CONTROLE SEGURANÇA operate at low level

    with high-level constructs
  24. The language

  25. Immutability

  26. Immutability

  27. Shadowing

  28. Shadowing

  29. The Character Type (UTF)

  30. The Tuple Type

  31. The Array Type

  32. Functions

  33. Statements and Expressions • Rust is an expression-based language, this

    is an important distinction to understand • Statements are instructions that perform some action and do not return a value ◦ Creating a variable and assigning a value to it with the let keyword ◦ Function definitions ◦ C and Ruby assignment returns the value of the assignment • Expressions evaluate to a resulting value ◦ the 6 in the statement let y = 6 ◦ Calling a function ◦ Calling a macro ◦ The block that we use to create new scopes, {} • Function bodies are made up of a series of statements optionally ending in an expression
  34. Functions

  35. Control Flow

  36. Control Flow

  37. Ownership is Rust’s most unique feature, and it enables Rust

    to make memory safety guarantees without needing a garbage collector
  38. Ownership • Heap: Memory set aside for dynamic allocation •

    Stack: Memory set aside for a thread • Ownership rules: ◦ Each value in Rust has a variable that's called its owner ◦ There can only be one owner at a time ◦ When the owner goes out of scope, the value will be dropped
  39. Referências

  40. None
  41. None
  42. References and Borrowing Como não tem ownership, mas é uma

    referência, não dropa ela aqui
  43. References and Borrowing Como tá fazendo borrowing, não pode modificar

  44. Mutable References Pode mudar se indicarmos que a referência é

    mutável. Mas só podemos ter uma referência mutável!
  45. Dangling References

  46. Structs

  47. None
  48. Method Syntax

  49. Pattern Matching

  50. Pattern Matching

  51. Matching with Option<T>

  52. Collections

  53. Vectors

  54. Hash Map

  55. Error Handling

  56. Unrecoverable Errors with panic!

  57. Recoverable Errors with Result

  58. Shortcuts for Panic on Error: unwrap and expect

  59. Generic Data Types

  60. None
  61. None
  62. Traits: Defining Shared Behavior

  63. Functional Stuff

  64. Closures and Iterators

  65. Ficou de fora • Slices • Associated Functions • Enums

    • Modules • Errors with unwrap and expect and ? • Lifetime (não entendi ainda) • Smart Pointers • Object Oriented Programming Features • Concorrência
  66. Onde aprender? https://doc.rust-lang.org/stable/book

  67. Onde aprender? • https://serokell.io/blog/learn-rust • http://intorust.com • https://stevedonovan.github.io/rust-gentle-intro/readme.html