Top Banner
Introduction to Rust Adityo Pratomo (@kotakmakan)
33

Introducing Rust

Jan 09, 2017

Download

Technology

Froyo Framework
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Introducing Rust

Introduction toRust

Adityo Pratomo(@kotakmakan)

Page 2: Introducing Rust

This Talk is AlsoAvailable atgithub.com/froyoframework/rust-intro/slide

Page 3: Introducing Rust

BackgroundChief Academic O�cer at FrameworkChief Technology O�cer at Labtek Indie

Page 4: Introducing Rust

FrameworkProviding software development course, training and workshopBased in BSD

Page 5: Introducing Rust

Labtek IndieRapid Prototyping As A ServiceBased in Bandung

Page 6: Introducing Rust

Codewise, I'mGeneralistCreative CoderC/C++, Java, JS

Page 7: Introducing Rust

Me and C++Met during undergrad (2005)Professionally using it since 2012Tool: openFrameworks, Cinder and Unreal Engine

Page 8: Introducing Rust

Interactive installation for Nike (2012)

Page 9: Introducing Rust
Page 10: Introducing Rust

C++ for Me(+) Fast product(+) Runs on many platforms(+) Robust debugging tool(-) Still have issues with pointers(-) Segfaults here and there

Page 11: Introducing Rust

When I MeetRust

System programming languageLike C++, without segfaults (yum!)Better handling of reference and pointersMixture of imperative and functional paradigm

Page 12: Introducing Rust

Why Rust?Easier to write secure codeEasier to write multithreaded code (hello concurrency)Runs on many devices/platforms

Page 13: Introducing Rust

What to Do withRust?

System programmingSomething low-level enough to bene�t from precise memorymanagement

Web Browser (Mozilla Firefox)Distributed storage system (Dropbox)3D GamesDevice driveOperating System

General tool

Page 14: Introducing Rust

Rust Comparedto Other

Direct alternative to C++Lower level access to Go/PythonNot just for network application like Node.js

Page 15: Introducing Rust

Rust's KillerFeatures

Type safetyTraits based genericsPattern matchingMemory safety

Page 16: Introducing Rust

How Rust is Fastand Safe?

Extensive compiler checkingFast: No garbage collection, Rust automatically detect when to freememory

Ownership of dataSafe: No data race, guaranteed data lifetime, no dangling-pointer

Ownership and Borrowing only allows one mutable reference(write access)

Page 17: Introducing Rust

A Tour of Rust'sSyntax

github.com/froyoframework/rust-intro/basic-rust-sample

Page 18: Introducing Rust

VariableVariable by default is immutableA binding to value exists

let angka = 9; let salam = "Selamat datang, Android no "; let halo = format!("{} {}", salam, angka); println!("{:?}", halo);

Page 19: Introducing Rust

FunctionReturn value in function is explicitly denoted using arrowThe returned value is the last variable stated without semicolon

let angka_saya = calc(angka);

fn calc(x: i32) -> i32 { let y; match x { 1...40 => y = 34, _ => y = 2, } y }

Page 20: Introducing Rust

StructA simple data structure that contains key-value entitiesEach key-value can use di�erent data Type

struct Pemain { nama: String, umur: i32, gol: i32, }

let buffon = Pemain {nama: "Buffon".to_string(), umur: 39, gol: 0 };

Page 21: Introducing Rust

Make Struct withFunction

fn tambah_pemain(nama_: &str, umur_: i32, gol_: i32) -> Pemain {

let pemain_saya = Pemain { nama: nama_.to_string(), umur: umur_, gol: gol_,

};

pemain_saya }

let ronaldo = tambah_pemain("Ronaldo", 31, 510);

Page 22: Introducing Rust

VectorArray-like structure that can be dynamically manipulated duringruntimeCan contain anything, from integers, �oats, Strings, to Structs

let deret = vec![1, 2, 3]; let mut himpunan = Vec::new(); himpunan.push(5); himpunan.push(6)

Page 23: Introducing Rust

Vector of Structsfn tambah_para_pemain() -> Vec<Pemain> { let ronaldo = tambah_pemain("Ronaldo", 31, 510); let bacca = tambah_pemain("Bacca", 31, 235); let payet = tambah_pemain("Payet", 28, 150);

let mut pemain_favorit = Vec::new(); pemain_favorit.push(ronaldo); pemain_favorit.push(bacca); pemain_favorit.push(payet);

pemain_favorit }

let pemain_keren = tambah_para_pemain();

Page 24: Introducing Rust

Ownership andBorrowing

A key concept that ensures safety and concurrency in RustBasically everytime a variable is used, its ownership is transferredto the one uses/calls itWhen an ownership is transferred, the old owner can't use theentity anymore

let pemain_bola = pemain_keren; println!("pemain pertama adalah: {}", pemain_keren[0].nama);

Page 25: Introducing Rust

Ownership andBorrowing

To solve the previous problem, Rust introduces BorrowingThis means that a variable can be borrowed, thus, it's still valid forbeing used elsewhereThis is accomplished by simple referencing that intended variable,thus the term "reference"

let pemain_bola = &pemain_keren; println!("pemain pertama adalah: {}", pemain_keren[0].nama);

Page 26: Introducing Rust

Ownership andBorrowing

Another thing that's correlated with referencing, is dereferencingThis means, accessing the value of a referenced variableBy default, a reference is immutableChange the value of a referenced variable by using mutablereference

let mut a = 90; let b = &mut a;

Page 27: Introducing Rust

Ownership andBorrowing

A consequence of borrowing, is the concept of a borrow lifetimeThis is denoted by a curly brace

let mut a = 90; { let b = &mut a; // a dipinjam di sini *b += 9; // isi a diakses di sini } // peminjaman a berakhir di sini println!("{}", a);

Page 28: Introducing Rust

Ownership andBorrowing

To ensure safety, the main rule in borrowing is:one or more references (&T) to a resource,exactly one mutable reference (&mut T).

Page 29: Introducing Rust

A Simple WebService

github.com/lunchboxav/rust-intro/webserver

Page 30: Introducing Rust

Why Learn NewLanguage?

Gains new perspective on how things are doneGains new understanding on programming itselfMake old and new things in a di�erent way

Page 31: Introducing Rust

Tips for LearningRust

Katas: learn by making familiar thingsTry make small tool to replace your existing toolConsult the documentationAsk people on SO/TwitterOrganize a community

Page 32: Introducing Rust

LearningResources

The Rust Book ( )Rust 101 ( )Rust Tutorial ( )Rust Syntax( )Rust By Example ( )Rustlings, small Rust Exercises( )24 Days of Rust ( )Rust FFI Omnibus ( )New Rustacean ( )

https://doc.rust-lang.org/book/https://www.ralfj.de/projects/rust-101/main.html

http://aml3.github.io/RustTutorial/html/toc.html

https://gist.github.com/brson/9dec4195a88066fa42e6http://rustbyexample.com/expression.html

https://github.com/carols10cents/rustlingshttp://zsiciarz.github.io/24daysofrust/

http://jakegoulding.com/rust-�-omnibus/http://www.newrustacean.com

Page 33: Introducing Rust

Thank [email protected]