プログラミング言語Rust @ GrandFrontend Osaka 2016

プログラミング言語Rust @ GrandFrontend Osaka 2016

0fa1c2ed2eb4a18ddec3dd70cb1f72db?s=128

Yosuke Onoue

August 28, 2016
Tweet

Transcript

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

  2. ࣗݾ঺հ • ͓ͷ͏͑ (@_likr) • ژ౎େֶ ֶࡍ༥߹ڭҭݚڀਪਐηϯλʔ
 ੓ࡦͷͨΊͷՊֶϢχοτ ಛఆॿڭ •

    ng-kyotoΦʔΨφΠβʔɺGDGਆށελοϑ • ՄࢹԽɺ࠷దԽɺΞϧΰϦζϜͷݚڀ • WebϑϩϯτΤϯυ։ൃ
  3. ϓϩάϥϜʹ͸ةݥ͕જΜͰ͍Δ

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

    console.log(2 * 3 + '4') // 64
  5. ͍ͭมߋ͞ΕΔ͔Θ͔Βͳ͍ 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 // }]
  6. ฒྻ؀ڥͰͷڝ߹ঢ়ଶ 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')
  7. ةݥ͕ةͳ͍

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

  9. JavaScriptͷ͜ͱ͸๨Εͯɺ

  10. Rust

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

    • ࠾༻ϓϩμΫτ • https://github.com/servo/servo • https://github.com/redox-os/redox
  12. Rustͷಛ௃ • ܕ҆શ • OwnershipɺLifetimesɺMutability • ϝλϓϩάϥϛϯά • GenericsɺTraitɺMacros •

    ύϑΥʔϚϯε • Low runtime overheadɺMulti threadɺSIMD
  13. Getting Started

  14. Install • Installer • https://www.rust-lang.org/ • rustup • https://www.rustup.rs/ •

    RustͷόʔδϣϯϚωʔδϟ • σΟϨΫτϦຖʹ΋όʔδϣϯઃఆՄೳ
  15. Rustup $ curl https://sh.rustup.rs -sSf | sh $ source ~/.cargo/env

    $ rustup install nightly $ rustup default nightly $ rustc --version
  16. Hello World $ rustc hello.rs $ ./hello Hello, world! 1

    fn main() { 2 println!("Hello, world!"); 3 } IFMMPST
  17. Cargo • Rust༻ύοέʔδϚωʔδϟ • ίϚϯυ • ϓϩδΣΫτ࡞੒ • Ϗϧυ /

    υΩϡϝϯτੜ੒ • ࣮ߦ / ςετ / ϕϯνϚʔΫ • ഑෍
  18. Hello World with Cargo $ cargo new --bin hello $

    cd hello $ cargo run Compiling hello v0.1.0 (…) Running `target/debug/hello` Hello, world!
  19. IronͰWebΞϓϦ • Iron : RustͷαʔόαΠυWebϑϨʔϜϫʔΫ • http://ironframework.io/ $ cargo new

    --bin iron-example $ cd iron-example
  20. 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<Response> { 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 <onoue@likr-lab.com>"] 5 6 [dependencies] 7 iron = "*" $BSHPUPNM TSDNBJOST
  21. 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
  22. Syntax

  23. 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 }
  24. 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 }
  25. 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 }
  26. 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 }
  27. 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 }
  28. 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 }
  29. 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 }
  30. 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 }
  31. 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 }
  32. 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 }
  33. generics 1 use std::fmt; 2 3 struct Point<T> { 4

    x: T, 5 y: T, 6 } 7 8 impl<T: fmt::Display> fmt::Display for Point<T> { 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 }
  34. More … • Standard Library • optionɺiterɺopsɺcmpɺ… • VecɺLinkedListɺHashMapɺHashSetɺ…) •

    BoxɺCellɺRcɺ… • Concurrency • threadɺOwnership
  35. Rustͷ఩ֶ • ίϯύΠϧ࣌ʹܾΊΒΕΔ͜ͱ͸ՄೳͳݶΓܾΊΔ • ܕͷϝϞϦαΠζ • ϝιουͷσΟεύον • ϦιʔεͷϥΠϑαΠΫϧ؅ཧ •

    ࣮ߦ࣌Φʔόʔϔουͷ࡟ݮ
  36. ·ͱΊ • Rust͸ߴ͍ϙςϯγϟϧΛൿΊͨϞμϯͳϓϩάϥϛϯάݴޠ • ڧ͍੩తܕ෇͚ • ߴ౓ͳந৅Խ • Ϟμϯͳݴޠػೳ •

    RustΛֶΜͰ҆શͰߴੑೳͳϓϩάϥϛϯάΛ͠Α͏ʂ
  37. Learn More • https://doc.rust-lang.org/book/ • https://doc.rust-lang.org/std/ • http://rustbyexample.com/ • https://crates.io/

  38. Enjoy Rust ! Thank you ?

  39. ͑ʁ

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

  41. RustͰϑϩϯτΤϯυ։ൃ • RustίϛϡχςΟ͸WebAssemblyରԠʹੵۃత • ҆શੑɺ଎౓໘ͷϝϦοτ • ΦϨͷߟ͑ͨ࠷ڧͷ࣍ੈ୅ϑϩϯτΤϯυ։ൃ
 Rust & Emscriptenͷ࿩


    https://speakerdeck.com/likr/rust-and-emscriptenfalsehua • asm.jsͱWebAssembly࣮ͬͯࡍͳΜͳͷʁ
 http://www.slideshare.net/likr/asmjswebassembly
  42. asm.jsͱWebAssembly • asm.js • ϒϥ΢β্Ͱߴ଎ಈ࡞ՄೳͳJavaScriptͷαϒηοτ • MozillaͷओಋͰීٴ • WebAssembly •

    ϒϥ΢β্Ͱ࣮ߦՄೳͳόΠφϦϑΥʔϚοτ • asm.jsͷܽ఺Λࠀ෰ • ֤ϒϥ΢βϕϯμ౳͕ڠۀͰ࢓༷ࡦఆ
  43. Emscripten • http://emscripten.org/ • C/C++ to asm.js compiler • LLVMϕʔε

    • WebGLαϙʔτ • Unity͔ΒͷWebग़ྗ • WebAssemblyαϙʔτ (experimental)
  44. 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
  45. WebAssemblyͷରԠঢ়گ • Chrome • chrome://flags/#enable-webassembly • Firefox • about:config javascript.options.wasm

    ΋͏ԕ͍ະདྷͷٕज़Ͱ͸ͳ͍ʂ
  46. Ͳ͜ʹ࢖͏ʁ • ϦονͳάϥϑΟοΫεͷήʔϜ • σʔλϏδϡΞϥΠθʔγϣϯπʔϧ • Պֶٕज़γϛϡϨʔγϣϯ • ը૾ɺԻ੠ɺಈըͷॲཧ •

    Τϯίʔυɺσίʔυ 8FCΞϓϦͷՄೳੑΛ޿͛Δʂ
  47. ·ͱΊ • Rust͸͍͍ͧ • WebAssemblyͷ࣮૷͸ண࣮ʹਐΜͰ͍Δ • RustͰWebͷ෦඼Λ࡞ΕΔະདྷ͸ͦ͏ԕ͘ͳ͍ʢ͔΋ • ະདྷʹඋ͑Α͏ •

    ͨ·ʹ͸WebͷະདྷΛߟ͑ͯΈΔͷ͸͍͔͕ʁ
  48. ະདྷͰձ͍·͠ΐ͏ Thank you !