Skip to content

Instantly share code, notes, and snippets.

@rsdy
Created July 23, 2020 10:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rsdy/7d93222bd9bcf851ab9239c38ac6d8ef to your computer and use it in GitHub Desktop.
Save rsdy/7d93222bd9bcf851ab9239c38ac6d8ef to your computer and use it in GitHub Desktop.

Rust 101

$whoami

@rhapsodhy

Startupok Rust + Security

https://ingraind.org https://github.com/rsdy/zerostash

Security

Microsoft: a biztonsági rések 70%-at memóriakezelési hibák okozzák

Google Chrome: tényleg annyi

use after free, buffer overflow, double free, memory race

Kényelem

Próbáltál mar Python/JavaScript unit teszteket irni?

C++-ban vagy Java-ban sem sokkal jobb a helyzet

Tesztelés/Validáció

Próbáltál mar Python/JavaScript unit teszteket irni?

C++-ban vagy Java-ban sem sokkal jobb a helyzet

Google: Go, Microsoft: C#

Biztonság: ✓

Kényelem: ¯\_ಠ_ಠ_/¯

Validáció: /m\

“most már két problémád van”

Garbage Collection

A GC algoritmusokat a környezetre kell hangolni, “GC pause”

Go-ban 10GB haszontalan memóriát allokálsz

Java-ban 10 millió paramétert írsz a run.sh-ba

Rust

“Az értelmetlen állapotokat nem tudjuk a programkódban kifejezni”

GC helyett RAII: “Resource acquisition is initialization”

Data race ellen élethossz típusparaméterek

Rust

Biztonság: ✓

Kényelem: ✓

Validáció: ✓

Hordozhatóság

LLVM, rustc

A cargo mindenre jó

Amire nem, arra van xargo

  • Cortex-M, foleg

–target=x86_64-unknown-linux-musl –target=armv7a-none-eabihf

Borrow checker

struct Point { x: i32, y: i32, z: i32 }

fn main() { let mut point = Point { x: 0, y: 0, z: 0 };

let borrowed = &point;

// ez nem megy csinalj_valami_okosat(&mut borrowed);

// ez sem valami_okosabbat(point); }

Algebrai Adattípusok

// tuple struct struct Point (i32, i32, i32 )

// struct struct Point { x: i32, y: i32, z: i32 }

// sum type: null ertek nincs enum Result<T, E> { Ok(T) Err(E), }

// magyar webre magyar tipust type Pont = Point;

// nincs null

Trait

Ruby/Python mixin Haskell typeclass Java interface

trait Stream<T> { fn next() -> T; }

impl<T> Stream<T> for Vec<T> where T: Serialize { fn next() -> T { … } }

RAII

pub fn read_json<T>(filename: impl AsRef<Path>) -> T { serde_json::from_reader(File::open(filename.as_ref()).unwrap()).unwrap() }

unwrap

Mivel nincsenek null értékek, ezért a Result/Option monadból ki kell szedni az értékeket

.unwrap().unwrap().unwrap()

? operátor nyelvi elem

pub fn commit_database(&mut self) -> Result<()> { let mut objstore = objects::Storage::get(self.backend.clone(), self.master_key.get_object_crypto()?);

objstore.commit(); Ok(()) }

Async/tobbszalusag

async fn notify_start(client: &reqwest::Client) { let res = client .post(“https://api.sendgrid.com/v3/mail/send”) .json(&json!( * blablabla * )) .send() .await;

match res { Err(e) => status_err!(“failed to send email: {}”, e), Ok(_) => (), } }

Problemak / threat model

cargo nélkul nehéz életre kelteni a fejlesztőkörnyezetet ==> Ennek megfelelően a cargo köré épült közösség a meghatározó

crates.io centralizált, és a GitHubon él

Bootstrappelt, Rustban írt fordító ==> Trusting (T)Rust?

Q & A

Kérdések

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment