Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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

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

Yosuke Onoue

August 28, 2016
Tweet

More Decks by Yosuke Onoue

Other Decks in Technology

Transcript

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

    ng-kyotoΦʔΨφΠβʔɺGDGਆށελοϑ • ՄࢹԽɺ࠷దԽɺΞϧΰϦζϜͷݚڀ • WebϑϩϯτΤϯυ։ൃ
  2. ͍ͭมߋ͞ΕΔ͔Θ͔Βͳ͍ 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 // }]
  3. ฒྻ؀ڥͰͷڝ߹ঢ়ଶ 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')
  4. Rust • Mozilla͕։ൃ͢ΔγεςϜϓϩάϥϛϯάݴޠ • https://www.rust-lang.org/ • ࠷৽όʔδϣϯ 1.11.0 • 6िؒͷϦϦʔεαΠΫϧ

    • ࠾༻ϓϩμΫτ • https://github.com/servo/servo • https://github.com/redox-os/redox
  5. Install • Installer • https://www.rust-lang.org/ • rustup • https://www.rustup.rs/ •

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

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

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

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

    cd hello $ cargo run Compiling hello v0.1.0 (…) Running `target/debug/hello` Hello, world!
  10. 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 <[email protected]>"] 5 6 [dependencies] 7 iron = "*" $BSHPUPNM TSDNBJOST
  11. 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
  12. 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 }
  13. 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 }
  14. 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 }
  15. 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 }
  16. 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 }
  17. 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 }
  18. 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 }
  19. 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 }
  20. 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 }
  21. 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 }
  22. 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 }
  23. RustͰϑϩϯτΤϯυ։ൃ • RustίϛϡχςΟ͸WebAssemblyରԠʹੵۃత • ҆શੑɺ଎౓໘ͷϝϦοτ • ΦϨͷߟ͑ͨ࠷ڧͷ࣍ੈ୅ϑϩϯτΤϯυ։ൃ
 Rust & Emscriptenͷ࿩


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

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

    • WebGLαϙʔτ • Unity͔ΒͷWebग़ྗ • WebAssemblyαϙʔτ (experimental)
  26. 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