Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

Alan R. Fachini alfakini alanfachini alfakini.com

Slide 3

Slide 3 text

Pq eu fui aprender Rust?

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

Introduction

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

Are we * yet?

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

$ cargo new hello_cargo $ cd hello_cargo

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

Ferries, the crab crustacean

Slide 22

Slide 22 text

Everything is about control and safety

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

The language

Slide 25

Slide 25 text

Immutability

Slide 26

Slide 26 text

Immutability

Slide 27

Slide 27 text

Shadowing

Slide 28

Slide 28 text

Shadowing

Slide 29

Slide 29 text

The Character Type (UTF)

Slide 30

Slide 30 text

The Tuple Type

Slide 31

Slide 31 text

The Array Type

Slide 32

Slide 32 text

Functions

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

Functions

Slide 35

Slide 35 text

Control Flow

Slide 36

Slide 36 text

Control Flow

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

Referências

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

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

Slide 43

Slide 43 text

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

Slide 44

Slide 44 text

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

Slide 45

Slide 45 text

Dangling References

Slide 46

Slide 46 text

Structs

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

Method Syntax

Slide 49

Slide 49 text

Pattern Matching

Slide 50

Slide 50 text

Pattern Matching

Slide 51

Slide 51 text

Matching with Option

Slide 52

Slide 52 text

Collections

Slide 53

Slide 53 text

Vectors

Slide 54

Slide 54 text

Hash Map

Slide 55

Slide 55 text

Error Handling

Slide 56

Slide 56 text

Unrecoverable Errors with panic!

Slide 57

Slide 57 text

Recoverable Errors with Result

Slide 58

Slide 58 text

Shortcuts for Panic on Error: unwrap and expect

Slide 59

Slide 59 text

Generic Data Types

Slide 60

Slide 60 text

No content

Slide 61

Slide 61 text

No content

Slide 62

Slide 62 text

Traits: Defining Shared Behavior

Slide 63

Slide 63 text

Functional Stuff

Slide 64

Slide 64 text

Closures and Iterators

Slide 65

Slide 65 text

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

Slide 66

Slide 66 text

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

Slide 67

Slide 67 text

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