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

Introduction to Rust

AF
July 14, 2021

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.

AF

July 14, 2021
Tweet

More Decks by AF

Other Decks in Programming

Transcript

  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

    View full-size slide

  2. Alan R. Fachini
    alfakini
    alanfachini
    alfakini.com

    View full-size slide

  3. Pq eu fui aprender Rust?

    View full-size slide

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

    View full-size slide

  5. Introduction

    View full-size slide

  6. Rust
    A system programming language focused on three
    goal: safety, speed, and concurrency.

    View full-size slide

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

    View full-size slide

  8. Are we * yet?

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  11. Tools
    ● rustup: the rust toolchain installer
    ● rustc: the rust compiler
    ● cargo: the package and project manager
    ● rustdoc: the documentation builder

    View full-size slide

  12. curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

    View full-size slide

  13. $ cargo new hello_cargo
    $ cd hello_cargo

    View full-size slide

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

    View full-size slide

  15. $ cargo build
    Compiling hello_cargo v0.1.0 (file:///projects/hello_cargo)
    Finished dev [unoptimized + debuginfo] target(s) in 2.85 secs

    View full-size slide

  16. $ cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.0 secs
    Running `target/debug/hello_cargo`
    Hello, world!

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  19. Ferries,
    the crab
    crustacean

    View full-size slide

  20. Everything is
    about control
    and safety

    View full-size slide

  21. Mais controle, mais segurança
    CONTROLE SEGURANÇA
    operate at low level with high-level constructs

    View full-size slide

  22. The language

    View full-size slide

  23. Immutability

    View full-size slide

  24. Immutability

    View full-size slide

  25. The Character Type (UTF)

    View full-size slide

  26. The Tuple Type

    View full-size slide

  27. The Array Type

    View full-size slide

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

    View full-size slide

  29. Control Flow

    View full-size slide

  30. Control Flow

    View full-size slide

  31. Ownership is Rust’s most
    unique feature, and it enables
    Rust to make memory safety
    guarantees without needing a
    garbage collector

    View full-size slide

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

    View full-size slide

  33. Referências

    View full-size slide

  34. References and Borrowing
    Como não tem
    ownership, mas é
    uma referência,
    não dropa ela
    aqui

    View full-size slide

  35. References and Borrowing
    Como tá fazendo
    borrowing, não
    pode modificar

    View full-size slide

  36. Mutable References
    Pode mudar se
    indicarmos que a
    referência é
    mutável.
    Mas só podemos
    ter uma
    referência
    mutável!

    View full-size slide

  37. Dangling References

    View full-size slide

  38. Method Syntax

    View full-size slide

  39. Pattern
    Matching

    View full-size slide

  40. Pattern Matching

    View full-size slide

  41. Matching with Option

    View full-size slide

  42. Error Handling

    View full-size slide

  43. Unrecoverable Errors with panic!

    View full-size slide

  44. Recoverable Errors with Result

    View full-size slide

  45. Shortcuts for Panic on Error: unwrap and expect

    View full-size slide

  46. Generic Data Types

    View full-size slide

  47. Traits: Defining Shared Behavior

    View full-size slide

  48. Functional
    Stuff

    View full-size slide

  49. Closures and Iterators

    View full-size slide

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

    View full-size slide

  51. Onde aprender?
    https://doc.rust-lang.org/stable/book

    View full-size slide

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

    View full-size slide