Slide 1

Slide 1 text

ϓϩάϥϛϯάݴޠRust ͓ͷ͏͑@_likr

Slide 2

Slide 2 text

ࣗݾ঺հ • ͓ͷ͏͑ (@_likr) • ژ౎େֶ ֶࡍ༥߹ڭҭݚڀਪਐηϯλʔ
 ੓ࡦͷͨΊͷՊֶϢχοτ ಛఆॿڭ • ng-kyotoΦʔΨφΠβʔɺGDGਆށελοϑ • ՄࢹԽɺ࠷దԽɺΞϧΰϦζϜͷݚڀ • WebϑϩϯτΤϯυ։ൃ

Slide 3

Slide 3 text

ϓϩάϥϜʹ͸ةݥ͕જΜͰ͍Δ

Slide 4

Slide 4 text

ҙਤ͠ͳ͍ܕͷૢ࡞ 1 console.log(2 * 3 + 4) // 10 2 console.log(2 * 3 + '4') // 64

Slide 5

Slide 5 text

͍ͭมߋ͞ΕΔ͔Θ͔Βͳ͍ 1 const d3 = require('d3') 2 3 const nodes = [{name: 'A'}, {name: 'B'}] 4 const links = [{source: 0, target: 1}] 5 d3.forceSimulation(nodes) 6 .force('link', d3.forceLink(links)) 7 8 console.log(nodes) 9 // [ 10 // {name: 'A', index: 0, x: 0, y: 0, vy: 0, vx: 0}, 11 // {name: 'B', index: 1, x: -7.373688780783198, … 12 // ] 13 console.log(links) 14 // [{ 15 // source: {name: 'A', index: 0, x: 0, y: 0, vy: 0, … 16 // target: {name: 'B', index: 1, x: … 17 // index: 0 18 // }]

Slide 6

Slide 6 text

ฒྻ؀ڥͰͷڝ߹ঢ়ଶ 1 const state = {value: 0} 2 3 const request = (url) => { 4 const currentValue = state.value 5 window.fetch(url) 6 .then((response) => response.json()) 7 .then((data) => { 8 state.value = currentValue + data.value 9 console.log(state.value) 10 }) 11 } 12 13 request('value.json') 14 request('value.json') 15 request('value.json') 16 request('value.json') 17 request('value.json')

Slide 7

Slide 7 text

ةݥ͕ةͳ͍

Slide 8

Slide 8 text

҆શͳϓϩάϥϜΛॻ͘ʹ͸Ͳ͏ͨ͠Βʁ ॻ͖΍ͯ͘͢ߴ଎ͩͱͳ͓خ͍͠

Slide 9

Slide 9 text

JavaScriptͷ͜ͱ͸๨Εͯɺ

Slide 10

Slide 10 text

Rust

Slide 11

Slide 11 text

Rust • Mozilla͕։ൃ͢ΔγεςϜϓϩάϥϛϯάݴޠ • https://www.rust-lang.org/ • ࠷৽όʔδϣϯ 1.11.0 • 6िؒͷϦϦʔεαΠΫϧ • ࠾༻ϓϩμΫτ • https://github.com/servo/servo • https://github.com/redox-os/redox

Slide 12

Slide 12 text

Rustͷಛ௃ • ܕ҆શ • OwnershipɺLifetimesɺMutability • ϝλϓϩάϥϛϯά • GenericsɺTraitɺMacros • ύϑΥʔϚϯε • Low runtime overheadɺMulti threadɺSIMD

Slide 13

Slide 13 text

Getting Started

Slide 14

Slide 14 text

Install • Installer • https://www.rust-lang.org/ • rustup • https://www.rustup.rs/ • RustͷόʔδϣϯϚωʔδϟ • σΟϨΫτϦຖʹ΋όʔδϣϯઃఆՄೳ

Slide 15

Slide 15 text

Rustup $ curl https://sh.rustup.rs -sSf | sh $ source ~/.cargo/env $ rustup install nightly $ rustup default nightly $ rustc --version

Slide 16

Slide 16 text

Hello World $ rustc hello.rs $ ./hello Hello, world! 1 fn main() { 2 println!("Hello, world!"); 3 } IFMMPST

Slide 17

Slide 17 text

Cargo • Rust༻ύοέʔδϚωʔδϟ • ίϚϯυ • ϓϩδΣΫτ࡞੒ • Ϗϧυ / υΩϡϝϯτੜ੒ • ࣮ߦ / ςετ / ϕϯνϚʔΫ • ഑෍

Slide 18

Slide 18 text

Hello World with Cargo $ cargo new --bin hello $ cd hello $ cargo run Compiling hello v0.1.0 (…) Running `target/debug/hello` Hello, world!

Slide 19

Slide 19 text

IronͰWebΞϓϦ • Iron : RustͷαʔόαΠυWebϑϨʔϜϫʔΫ • http://ironframework.io/ $ cargo new --bin iron-example $ cd iron-example

Slide 20

Slide 20 text

IronͰWebΞϓϦ 1 extern crate iron; 2 3 use iron::prelude::*; 4 use iron::status; 5 6 fn main() { 7 fn hello_world(_: &mut Request) -> IronResult { 8 Ok(Response::with((status::Ok, "Hello World!"))) 9 } 10 11 Iron::new(hello_world).http("localhost:3000").unwrap(); 12 println!("On 3000"); 13 } 1 [package] 2 name = "iron-example" 3 version = "0.1.0" 4 authors = ["Yosuke ONOUE "] 5 6 [dependencies] 7 iron = "*" $BSHPUPNM TSDNBJOST

Slide 21

Slide 21 text

IronͰWebΞϓϦ $ cargo run Compiling winapi v0.2.8 Compiling unicode-normalization v0.1.2 Compiling semver v0.1.20 … Compiling iron-example v0.1.0 (…) Running `target/debug/iron-example` On 3000

Slide 22

Slide 22 text

Syntax

Slide 23

Slide 23 text

Variable Bindings 1 fn main() { 2 let a = 42; 3 let b = 8.3; 4 let c = true; 5 let d = "hello"; 6 let e = [1, 2, 3]; 7 let f = (4, "5"); 8 9 println!("{} {} {} {} {:?} {:?}", a, b, c, d, e, f); 10 // 42 8.3 true hello [1, 2, 3] (4, "5") 11 }

Slide 24

Slide 24 text

Mutability 1 fn main() { 2 let a = [1, 2, 3]; 3 // error 4 // a = [4, 5, 6]; 5 // a[1] = 83; 6 7 let mut b = [1, 2, 3]; 8 b = [4, 5, 6]; 9 b[1] = 83; 10 11 println!("{:?} {:?}", a, b); 12 }

Slide 25

Slide 25 text

Functions 1 fn print_hello() { 2 println!("Hello"); 3 } 4 5 fn print_twice(x: i32) { 6 let y = x * 2; 7 println!("{}", y); 8 } 9 10 fn twice(x: i32) -> i32 { 11 x * 2 12 } 13 14 fn main() { 15 print_hello(); 16 print_twice(5); 17 println!("{}", twice(5)); 18 }

Slide 26

Slide 26 text

if … else … 1 fn main() { 2 let x = 83; 3 4 if x >= 0 { 5 println!("positive"); 6 } else { 7 println!("negative"); 8 } 9 10 let y = if x % 3 == 0 { 11 "red" 12 } else if x % 3 == 1 { 13 "green" 14 } else { 15 "blue" 16 }; 17 println!("{}", y); 18 }

Slide 27

Slide 27 text

loop 1 fn main() { 2 let mut repeat = true; 3 while repeat { 4 println!("loop"); 5 repeat = false; 6 } 7 8 let vals = [1, 2, 3]; 9 for x in vals.iter() { 10 println!("{}", x); 11 } 12 13 loop { 14 println!("loop"); 15 break; 16 } 17 }

Slide 28

Slide 28 text

match 1 fn print_sign(x: i32) { 2 match x { 3 0 => println!("zero"), 4 val if val > 0 => println!("positive"), 5 _ => println!("negative"), 6 } 7 } 8 9 fn main() { 10 print_sign(0); 11 print_sign(83); 12 print_sign(-10); 13 }

Slide 29

Slide 29 text

enum 1 enum Shape { 2 Rect (i32, i32, u32, u32), // (x, y, width, height) 3 Circle {x: i32, y: i32, r: u32}, 4 Empty, 5 } 6 7 fn print_shape(shape: Shape) { 8 match shape { 9 Shape::Rect (x, y, w, h) => println!("Rect {} {} {} {}", x, y, w, h), 10 Shape::Circle {x, y, r} => println!("Circle {} {} {}", x, y, r), 11 _ => println!("Other"), 12 } 13 } 14 15 fn main() { 16 print_shape(Shape::Rect (10, -20, 50, 50)); 17 print_shape(Shape::Circle {x: -10, y: 0, r: 5}); 18 print_shape(Shape::Empty); 19 }

Slide 30

Slide 30 text

struct 1 struct Point { 2 x: i32, 3 y: i32, 4 } 5 6 fn main() { 7 let a = Point {x: 10, y: 10}; 8 let mut b = Point {x: 20, .. a}; 9 println!("x = {}, y = {}", a.x, a.y); 10 b.y = 40; 11 println!("x = {}, y = {}", b.x, b.y); 12 }

Slide 31

Slide 31 text

method 1 struct Point { 2 x: f32, 3 y: f32, 4 } 5 6 impl Point { 7 fn distance_from_origin(&self) -> f32 { 8 f32::sqrt(self.x * self.x + self.y * self.y) 9 } 10 } 11 12 fn main() { 13 let a = Point {x: 10.0, y: 10.0}; 14 println!("{}", a.distance_from_origin()); 15 }

Slide 32

Slide 32 text

trait 1 use std::ops::Add; 2 3 struct Point { 4 x: f32, 5 y: f32, 6 } 7 8 impl Add for Point { 9 type Output = Point; 10 fn add(self, other: Point) -> Point { 11 Point {x: self.x + other.x, y: self.y + other.y} 12 } 13 } 14 15 fn main() { 16 let a = Point {x: 10.0, y: 10.0}; 17 let b = Point {x: 20.0, y: 30.0}; 18 let c = a + b; 19 println!("x = {}, y = {}", c.x, c.y); 20 }

Slide 33

Slide 33 text

generics 1 use std::fmt; 2 3 struct Point { 4 x: T, 5 y: T, 6 } 7 8 impl fmt::Display for Point { 9 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 10 write!(f, "({}, {})", self.x, self.y) 11 } 12 } 13 14 fn main() { 15 let a = Point {x: 10, y: 10}; 16 let b = Point {x: 2.5, y: 8.3}; 17 let c = Point {x: "1", y: "2"}; 18 println!("{} {} {}", a, b, c); 19 }

Slide 34

Slide 34 text

More … • Standard Library • optionɺiterɺopsɺcmpɺ… • VecɺLinkedListɺHashMapɺHashSetɺ…) • BoxɺCellɺRcɺ… • Concurrency • threadɺOwnership

Slide 35

Slide 35 text

Rustͷ఩ֶ • ίϯύΠϧ࣌ʹܾΊΒΕΔ͜ͱ͸ՄೳͳݶΓܾΊΔ • ܕͷϝϞϦαΠζ • ϝιουͷσΟεύον • ϦιʔεͷϥΠϑαΠΫϧ؅ཧ • ࣮ߦ࣌Φʔόʔϔουͷ࡟ݮ

Slide 36

Slide 36 text

·ͱΊ • Rust͸ߴ͍ϙςϯγϟϧΛൿΊͨϞμϯͳϓϩάϥϛϯάݴޠ • ڧ͍੩తܕ෇͚ • ߴ౓ͳந৅Խ • Ϟμϯͳݴޠػೳ • RustΛֶΜͰ҆શͰߴੑೳͳϓϩάϥϛϯάΛ͠Α͏ʂ

Slide 37

Slide 37 text

Learn More • https://doc.rust-lang.org/book/ • https://doc.rust-lang.org/std/ • http://rustbyexample.com/ • https://crates.io/

Slide 38

Slide 38 text

Enjoy Rust ! Thank you ?

Slide 39

Slide 39 text

͑ʁ

Slide 40

Slide 40 text

ϑϩϯτΤϯυͷ࿩͠ΖΑʁ

Slide 41

Slide 41 text

RustͰϑϩϯτΤϯυ։ൃ • RustίϛϡχςΟ͸WebAssemblyରԠʹੵۃత • ҆શੑɺ଎౓໘ͷϝϦοτ • ΦϨͷߟ͑ͨ࠷ڧͷ࣍ੈ୅ϑϩϯτΤϯυ։ൃ
 Rust & Emscriptenͷ࿩
 https://speakerdeck.com/likr/rust-and-emscriptenfalsehua • asm.jsͱWebAssembly࣮ͬͯࡍͳΜͳͷʁ
 http://www.slideshare.net/likr/asmjswebassembly

Slide 42

Slide 42 text

asm.jsͱWebAssembly • asm.js • ϒϥ΢β্Ͱߴ଎ಈ࡞ՄೳͳJavaScriptͷαϒηοτ • MozillaͷओಋͰීٴ • WebAssembly • ϒϥ΢β্Ͱ࣮ߦՄೳͳόΠφϦϑΥʔϚοτ • asm.jsͷܽ఺Λࠀ෰ • ֤ϒϥ΢βϕϯμ౳͕ڠۀͰ࢓༷ࡦఆ

Slide 43

Slide 43 text

Emscripten • http://emscripten.org/ • C/C++ to asm.js compiler • LLVMϕʔε • WebGLαϙʔτ • Unity͔ΒͷWebग़ྗ • WebAssemblyαϙʔτ (experimental)

Slide 44

Slide 44 text

Rust to asm.js / WebAssembly • ASM.JS code Using Rust and Emscripten
 http://ashleysommer.com.au/how-to/articles/asm-js-code-using- rust-and-emscripten • Dockerfile
 https://github.com/likr/docker-rust-emscripten • binaryen
 Compiler infrastructure and toolchain library for WebAssembly, in C++
 https://github.com/WebAssembly/binaryen • mir2wasm
 An experimental compiler from Rust to WebAssembly
 https://github.com/brson/mir2wasm

Slide 45

Slide 45 text

WebAssemblyͷରԠঢ়گ • Chrome • chrome://flags/#enable-webassembly • Firefox • about:config javascript.options.wasm ΋͏ԕ͍ະདྷͷٕज़Ͱ͸ͳ͍ʂ

Slide 46

Slide 46 text

Ͳ͜ʹ࢖͏ʁ • ϦονͳάϥϑΟοΫεͷήʔϜ • σʔλϏδϡΞϥΠθʔγϣϯπʔϧ • Պֶٕज़γϛϡϨʔγϣϯ • ը૾ɺԻ੠ɺಈըͷॲཧ • Τϯίʔυɺσίʔυ 8FCΞϓϦͷՄೳੑΛ޿͛Δʂ

Slide 47

Slide 47 text

·ͱΊ • Rust͸͍͍ͧ • WebAssemblyͷ࣮૷͸ண࣮ʹਐΜͰ͍Δ • RustͰWebͷ෦඼Λ࡞ΕΔະདྷ͸ͦ͏ԕ͘ͳ͍ʢ͔΋ • ະདྷʹඋ͑Α͏ • ͨ·ʹ͸WebͷະདྷΛߟ͑ͯΈΔͷ͸͍͔͕ʁ

Slide 48

Slide 48 text

ະདྷͰձ͍·͠ΐ͏ Thank you !