+ All Categories
Home > Software > Introducing Rust

Introducing Rust

Date post: 11-Jan-2017
Category:
Upload: adityo-pratomo
View: 45 times
Download: 0 times
Share this document with a friend
33
Introduction to Rust Adityo Pratomo (@kotakmakan)
Transcript

Introduction toRust

Adityo Pratomo(@kotakmakan)

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

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

FrameworkProviding software development course, training and workshopBased in BSD

Labtek IndieRapid Prototyping As A ServiceBased in Bandung

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

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

Interactive installation for Nike (2012)

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

When I MeetRust

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

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

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

Rust Comparedto Other

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

Rust's KillerFeatures

Type safetyTraits based genericsPattern matchingMemory safety

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)

A Tour of Rust'sSyntax

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

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);

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 }

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 };

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);

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)

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();

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);

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);

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;

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);

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

A Simple WebService

github.com/lunchboxav/rust-intro/webserver

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

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

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

Thank [email protected]


Recommended