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

Rust 探訪 / rust-chotto-wakaru

やし
November 18, 2022

Rust 探訪 / rust-chotto-wakaru

やし

November 18, 2022
Tweet

More Decks by やし

Other Decks in Programming

Transcript

  1. Rust ୳๚ 2022.11.18 ΪϑςΟ techbash @yashi848484 1

  2. Introduction • Rust ΛֶͿ͖͔͚͕ͬ WebAssembly ͩͬͨͨΊɺwasm ͷ࿩Λͯ͠ ͔Β Rust ͷ࿩Λͯ͠Έ͍ͨͱࢥ͍·͢

    2
  3. WebAssembly 3

  4. WebAssembly • Web্(≒ϒϥ΢β্)Ͱಈ͘όΠφϦܗࣜ • ߴ଎ɺߴޮ཰ɺϙʔλϒϧ • js 㱻 wasm ؒͰ΍ΓͱΓ͕Ͱ͖Δ

    • js ͷஔ͖׵͑Λ໨ࢦ͍ͯ͠ΔΘ͚Ͱ͸ͳ͍ 4
  5. https://webassembly.org/getting-started/developers-guide/ 5

  6. WebAssemblyςΩετܗࣜ (module (func $i (import "imports" "imported_func") (param i32)) (func

    (export "exported_func") i32.const 42 call $i ) ) 6
  7. WebAssembly • jsͰߴෛՙͳॲཧΛ΍Ζ͏ͱ͢Δͱਏ͍ • VR, AR, ը૾ฤूɺήʔϜͳͲ • wasmपลͷΤίγεςϜ͸·ͩᴈ໌ظ 7

  8. js͔Β wasm Λ࢖͏ • import puyo from 'path/to/wasm/module'; • ͱ͸Ͱ͖ͳ͍ʂ

    • fetch→instanciate→function call 8
  9. js͔Β wasm Λ࢖͏ྫ const importObject = { imports: { imported_func:

    (arg) => console.log(arg) } }; WebAssembly.instantiateStreaming(fetch("simple.wasm"), importObject).then( (obj) => obj.instance.exports.exported_func() ); 9
  10. WASI • "Web"Assembly ͸ Web ͚ͩͷ΋ͷʹ͠ͳͯ͘΋ྑ͍ͷͰ͸ʁ • →ଞͷ؀ڥͰ΋ಈ͔ͦ͏ • OSͱͷ΍ΓͱΓ=γεςϜίʔϧ͕ඞཁ

    • OSந৅Խ૚ͱͯ͠ͷ࢓༷ఆٛ • wasi-core, wasi-libc ͳͲϞδϡʔϧϕʔεͰ֦ுத • WASI ରԠ wasm ͳΒϒϥ΢β֎Ͱ΋࣮ߦՄೳɻݱࡏCͱRustͷΈରԠ 10
  11. Rust 11

  12. Rust • 2010- • Rust Foundation(2021-) • ύϑΥʔϚϯε΍৴པੑ(ϝϞϦ҆શੑ)Λॏࢹ • ςετ༻ߏจΛಉࠝ

    12
  13. Rust • جຊతʹʮݻ͘ʯॻ͘ݴޠ • ௿ϨΠϠʔΛ࿔ΔͨΊͷݴޠɺͱࢥͬͪΌͬͯྑ͍͔΋ • จ๏ଟ͍ 13

  14. 14

  15. Χχʁ • ʮRustΛॻ͘ਓʯ= rustacean • crustacean ͕༝དྷ 15

  16. Χχʁ • ඇެࣜϚείοτ ferris • ferrous ͕༝དྷɻḊ͸మʹͰ͖Δɻ • ެࣜυΩϡϝϯτʹ͕ͬͭΓొ৔͢Δ(ඇެࣜͱ͸) 16

  17. Rustपลπʔϧ • rustup • cargo • clippy 17

  18. Rustup • Rust ͷπʔϧνΣʔϯΠϯετʔϥʔ • Rust ͷΠϯετʔϧɺόʔδϣϯΞοϓͳͲ͕Ͱ͖Δ • Rust Λ࢖͏ͨΊʹ·ͣ࠷ॳʹΠϯετʔϧ͢Δπʔϧ

    18
  19. Cargo • Rust ͷύοέʔδϚωʔδϟʔ • ϓϩδΣΫτ࡞੒ɺґଘؔ܎؅ཧɺϏϧυɺςετ࣮ߦɺυΩϡϝϯ τੜ੒ͳͲ͍Ζ͍Ζ • rustupͰRustΛΠϯετʔϧ͢Δͱ෇͍ͯ͘Δ 19

  20. Clippy • Rust ͷ Linter • ඞਢͰ͸ͳ͍͕͋Δͱ݁ߏศར 20

  21. RustΤσΟγϣϯ • RustͷϦϦʔεαΠΫϧ͸6िؒ • 2~3೥͝ͱʹ৽͍͠ΤσΟγϣϯ͕࡞ΒΕΔ • Cargo.toml ͰϓϩδΣΫτͰར༻͢ΔΤσΟγϣϯΛઃఆ͢Δ • ݱࡏ(202211)͋ΔΤσΟγϣϯ͸2015,

    2018, 2021 21
  22. RustͷߏจΛͪΐͬͱ͚ͩ೷͍ͯΈΑ͏ • Ownership • Pattern Matching • Trait 22

  23. Ownership 23

  24. Ownership ͱ͸ • ஋͸ॴ༗ऀ͕࣋ͭ • ͋Δ஋ʹର͢Δॴ༗ऀ͸ඞͣҰ͚ͭͩ • ॴ༗ऀ͕είʔϓ͔Β֎ΕͨΒɺ஋͸ഁغ͞ΕΔ 24

  25. Ownership ͷϞνϕʔγϣϯ • ϓϩάϥϜʹ͓͚ΔϝϞϦ؅ཧΛָʹݫີʹ΍Γ͍ͨ • ΨϕʔδίϨΫγϣϯ: ੍ָ͕ͩޚ͠ʹ͍͘ • ໌ࣔతʹϝϞϦ֬อ/։์͢Δ: ݫີ͕ͩେม

    25
  26. Ownership { // a͸ɺ͜͜Ͱ͸༗ޮͰ͸ͳ͍ɻ·ͩએݴ͞Ε͍ͯͳ͍ let a = 5; // a͸ɺ͔͜͜Β༗ޮʹͳΔ

    // aͰ࡞ۀΛ͢Δ } // ͜ͷείʔϓ͸ऴΘΓɻ΋͏a͸༗ޮͰ͸ͳ͍ 26
  27. Ownership(୅ೖ) { let x = 5; let y = x;

    println!("x = {}, y = {}", x, y); } 27
  28. Ownership(୅ೖ) { let s1 = String::from("hello"); let s2 = s1;

    println!("{}, world!", s1); } 28
  29. Ownership(୅ೖ) • ઌͷίʔυ͸ಈ͔ͳ͍ʂ • จࣈྻ͸஋ͷϙΠϯλΛ࣋ͭ • ୅ೖ͢ΔͱϙΠϯλ͕ίϐʔ͞ΕΔ(shallow copy) • Rust

    Ͱ͸ɺs1 Λ s2 ʹ୅ೖͨ࣌͠఺Ͱจࣈྻͷॴ༗ऀ͕ s2 ʹҠΔ ˠmove 29
  30. Ownership(୅ೖ) • ͋Εʁ੔਺ͷͱ͖͸ಈ͍͚ͨͲɾɾɾʁ • ੔਺ͷ৔߹ɺshallow copy ͱ deep copy ͷ۠ผ͕ແ͍(ཁΒͳ͍)

    • ۠ผ͕ཁΒͳ͍ܕͷྫ: u32, bool, char ͳͲ 30
  31. Ownership(ؔ਺) • ؔ਺ʹ஋Λ౉ͨ͠Γɺؔ਺͔Β஋Λฦͨ͠Γͯ͠΋ɺϜʔϒ͞ΕΔ 31

  32. Ownership(ؔ਺) { let s = String::from("hello"); // s͕είʔϓʹೖΔ fn_hoge(s); //

    sͷ஋͕ؔ਺ʹϜʔϒ͞Ε... // ... ͜͜Ͱ͸΋͏༗ޮͰ͸ͳ͍ let x = 5; // x͕είʔϓʹೖΔ fn_fuga(x); // x΋ؔ਺ʹϜʔϒ͞ΕΔ͕ɺ // i32͸CopyͳͷͰɺ͜ͷޙʹxΛ࢖ͬͯ΋େৎ෉ } // ͜͜Ͱx͕είʔϓΛൈ͚ɺs΋είʔϓΛൈ͚Δɻͨͩ͠ɺsͷ஋͸Ϝʔϒ͞Ε͍ͯΔͷͰɺԿ΋ಛผͳ͜ͱ͸ى͜Βͳ͍ɻ 32
  33. Ownership(ؔ਺) fn main() { let s1 = gives_ownership(); // gives_ownership͸ɺ໭Γ஋Λs1ʹϜʔϒ͢Δ

    let s2 = String::from("hello"); // s2͕είʔϓʹೖΔ let s3 = takes_and_gives_back(s2); // s2͸takes_and_gives_backʹϜʔϒ͞Ε໭Γ஋΋s3ʹϜʔϒ͞ΕΔ } // ͜͜Ͱɺs3͸είʔϓΛൈ͚υϩοϓ͞ΕΔɻs2΋είʔϓΛൈ͚Δ͕ɺϜʔϒ͞Ε͍ͯΔͷͰԿ΋ى͖ͳ͍ɻs1΋είʔϓΛൈ͚υϩοϓ͞ΕΔɻ fn gives_ownership() -> String { let some_string = String::from("hello"); // some_string͕είʔϓʹೖΔ some_string // some_string͕ฦ͞Εɺݺͼग़͠ݩؔ਺ʹϜʔϒ͞ΕΔ } fn takes_and_gives_back(a_string: String) -> String { // a_string͕είʔϓʹೖΔɻ a_string // a_string͕ฦ͞Εɺݺͼग़͠ݩؔ਺ʹϜʔϒ͞ΕΔ } 33
  34. Ownership(ؔ਺) • ؔ਺ʹ஋Λ౉͢ͱ͍͍ͪͪϜʔϒ͞ΕΔͷ໘౗...ɻ • →஋Ͱ͸ͳ͘஋ͷࢀরΛ౉͢͜ͱ͕Ͱ͖Δ • ؔ਺ͷҾ਺ʹࢀরΛऔΔ͜ͱ=आ༻ͱݺͿ 34

  35. Ownership(ࢀর) fn main() { let s1 = String::from("hello"); let len

    = calculate_length(&s1); } fn calculate_length(s: &String) -> usize { // s͸String΁ͷࢀর s.len() } // ͜͜Ͱɺs͸είʔϓ֎ʹͳΔ͕ɺࢀর͍ͯ͠Δ΋ͷͷॴ༗ݖΛ͍࣋ͬͯΔΘ͚Ͱ͸ͳ͍ͷͰԿ΋ى͜Βͳ͍ 35
  36. Ownership(ࢀর) fn main() { let s = String::from("hello"); change(&s); }

    // आ༻ͨ͠Կ͔Λมߋ͍ͨ͠ʂ fn change(some_string: &String) { some_string.push_str(", world"); } 36
  37. Ownership(ࢀর) fn main() { // ม਺͸σϑΥϧτͰෆมͳͨΊɺmutΛ෇༩ let mut s =

    String::from("hello"); change(&mut s); } fn change(some_string: &mut String) { some_string.push_str(", world"); } 37
  38. Pattern Matching 38

  39. Pattern Matching • ύλʔϯͱ஋Λൺֱ͠ɺϚον݁ՌʹԠͯ͡ॲཧΛߦ͏ 39

  40. Pattern Matching enum Coin { Penny, Nickel, Dime, Quarter, }

    fn value_in_cents(coin: Coin) -> u32 { match coin { Coin::Penny => 1, Coin::Nickel => 5, Coin::Dime => 10, Coin::Quarter => 25, } } 40
  41. Pattern Matching • ύλʔϯ͸ॱʹॲཧ͞Εɺ࠷ॳʹҰகͨࣜ͠ͷ݁ՌΛฦ͢ • Ұகͨ͠ΒҎ߱ͷΞʔϜ(ύλʔϯ => ࣜ)͸ॲཧ͞Εͳ͍(୹བྷධՁ) • matchࣜ͸໢ཏతͰͳ͚Ε͹ͳΒͳ͍

    • _ ͸ԿʹͰ΋Ϛον͢Δ 41
  42. Pattern Matching(Ψʔυ) match x { 0 => println!("zero"), 1 =>

    println!("one"), n => if is_prime(n) { println!("prime number {}", n); } else { println!("composite number {}", n); } } match x { 0 => println!("zero"), 1 => println!("one"), n if is_prime(n) => println!("prime number {}", n), n => println!("composite number {}", n) } 42
  43. Pattern Matching(Ψʔυ) • ͋Δ৚݅Λຬͨ͢΋ͷͷΈΛϚονͤ͞Δ͜ͱ͕Ͱ͖Δ(Ψʔυ) 43

  44. Optionܕ • Rust ʹ͸ null ͕ͳ͍ • ͨͩ͠ʮແޮͳ஋ʯΛද͢ํ๏͕༻ҙ͞Ε͍ͯΔ 44

  45. Optionܕ enum Option<T> { Some(T), // ༗ޮͳ஋ None, // ແޮͳ஋

    } 45
  46. Pattern Matching(෦෼ଋറ) fn plus_one(x: Option<i32>) -> Option<i32> { match x

    { None => None, Some(i) => Some(i + 1), } } let five = Some(5); let six = plus_one(five); let none = plus_one(None); 46
  47. Pattern Matching(෦෼ଋറ) • ύλʔϯʹϚονͨ͠ʮ͋Δ෦෼ʯΛผͷม਺ʹଋറͰ͖Δ 47

  48. Pattern Matching(ม਺) struct Point { x: i32, y: i32, }

    let ((hoge, fuga), Point {a, b}) = ((3, 10), Point { x: 3, y: -10 }); 48
  49. Pattern Matching • ม਺એݴͷͱ͖ʹ΋Pattern Matching ͕Ͱ͖Δ 49

  50. Trait 50

  51. Traitͱ͸ • ʮಛఆͷৼΔ෣͍Λ࣋ͭܕʯΛఆٛͰ͖Δ • Java ͷΠϯλʔϑΣʔεɺSwift ͷϓϩτίϧʹ͍ۙ • ʮܕͷάϧʔϐϯάʯ͕Ϟνϕʔγϣϯ(ͨͿΜͶ) 51

  52. Trait(ఆٛ) trait Purchasable { fn get_subtotal_price(&self) -> f64; } 52

  53. Trait(࣮૷) struct IceCream { unit_price: f64, flavor: String, } struct

    EnglishClass { hourly_price: f64, hour: f64, difficulty_level: String, } impl Purchasable for IceCream { fn get_subtotal_price(&self) -> f64 { self.unit_price } } impl Purchasable for EnglishClass { fn get_subtotal_price(&self) -> f64 { &self.hourly_price * &self.hour } } 53
  54. Trait • Trait Λ࣮૷͢Δܕશͯʹରͯ͠ɺશͯͷϝιουͷ࣮૷Λཁٻ͢Δͷ ͸େม • →σϑΥϧτ࣮૷ΛఆٛͰ͖Δ • ඞཁʹԠͯ͡ΦʔόʔϥΠυͰ͖Δ 54

  55. Trait(σϑΥϧτ࣮૷) const DEFAULT_CONSUMPTION_TAX_RATE: f64 = 10.0; trait Purchasable { fn

    get_subtotal_price(&self) -> f64; // σϑΥϧτ࣮૷͋Γ fn get_tax_included_price(&self) -> f64 { let subtotal = &self.get_subtotal_price(); subtotal / DEFAULT_CONSUMPTION_TAX_RATE + subtotal } } 55
  56. Trait(σϑΥϧτ࣮૷) fn main() { let vanilla_ice = IceCream { unit_price:

    400.0, flavor: String::from("vanilla"), }; let some_class = EnglishClass { hourly_price: 2000.0, hour: 3.0, difficulty_level: String::from("hard"), }; println!("{}", vanilla_ice.get_tax_included_price()); // 440 println!("{}", some_class.get_tax_included_price()); // 6600 } 56
  57. Trait • ؔ਺ͷҾ਺΍ฦΓ஋ʹɺʮಛఆͷTraitΛ࣮૷͍ͯ͠ΔܕʯΛࢦఆ͢Δ ͜ͱ͕Ͱ͖Δ 57

  58. Trait(ؔ਺) fn get_tax_amount(arg: &impl Purchasable) -> f64 { let tax_included_price

    = arg.get_tax_included_price(); let subtotal = arg.get_subtotal_price(); tax_included_price - subtotal } // δΣωϦοΫܕΛ࢖ͬͯॻ͘͜ͱ΋Ͱ͖Δ fn get_tax_amount<T: Purchasable>(arg: &T) -> f64 { let tax_included_price = arg.get_tax_included_price(); let subtotal = arg.get_subtotal_price(); tax_included_price - subtotal } 58
  59. Trait(ؔ਺) fn get_vanilla_ice() -> impl Purchasable { IceCream { unit_price:

    400.0, flavor: String::from("vanilla"), } } fn main() { let vanilla_ice = get_vanilla_ice(); // vanilla_ice ͷܕ͸ impl Purchasable } 59
  60. ·ͱΊ • ஌Βͳ͍͏ͪʹ2021ΤσΟγϣϯ͕Ͱ͖͍ͯͯϏϏΓ·ͨ͠ • ࠓ೔঺հͨ͠ߏจ͸ණࢁͷҰ֯Ͱ͢ • ֶशۂઢ͕ٸफ़ͱݴΘΕΔ Rust ͷϋʔυϧ͕গ͠Ͱ΋Լ͕Ε͹޾͍ Ͱ͢

    60
  61. ࢀߟ • https://webassembly.org/ • https://developer.mozilla.org/ja/docs/WebAssembly • https://doc.rust-jp.rs/book-ja/ • https://numb86-tech.hatenablog.com/entry/2021/09/14/210955 •

    https://gist.github.com/omasanori/7858569 • https://zenn.dev/mebiusbox/books/22d4c1ed9b0003 61