$30 off During Our Annual Pro Sale. View Details »

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

    View Slide

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

    View Slide

  3. WebAssembly
    3

    View Slide

  4. WebAssembly
    • Web্(≒ϒϥ΢β্)Ͱಈ͘όΠφϦܗࣜ


    • ߴ଎ɺߴޮ཰ɺϙʔλϒϧ


    • js 㱻 wasm ؒͰ΍ΓͱΓ͕Ͱ͖Δ


    • js ͷஔ͖׵͑Λ໨ࢦ͍ͯ͠ΔΘ͚Ͱ͸ͳ͍
    4

    View Slide

  5. https://webassembly.org/getting-started/developers-guide/
    5

    View Slide

  6. WebAssemblyςΩετܗࣜ
    (module


    (func $i (import "imports" "imported_func") (param i32))


    (func (export "exported_func")


    i32.const 42


    call $i


    )


    )


    6

    View Slide

  7. WebAssembly
    • jsͰߴෛՙͳॲཧΛ΍Ζ͏ͱ͢Δͱਏ͍


    • VR, AR, ը૾ฤूɺήʔϜͳͲ


    • wasmपลͷΤίγεςϜ͸·ͩᴈ໌ظ
    7

    View Slide

  8. js͔Β wasm Λ࢖͏
    • import puyo from 'path/to/wasm/module';


    • ͱ͸Ͱ͖ͳ͍ʂ


    • fetch→instanciate→function call
    8

    View Slide

  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

    View Slide

  10. WASI
    • "Web"Assembly ͸ Web ͚ͩͷ΋ͷʹ͠ͳͯ͘΋ྑ͍ͷͰ͸ʁ


    • →ଞͷ؀ڥͰ΋ಈ͔ͦ͏


    • OSͱͷ΍ΓͱΓ=γεςϜίʔϧ͕ඞཁ


    • OSந৅Խ૚ͱͯ͠ͷ࢓༷ఆٛ


    • wasi-core, wasi-libc ͳͲϞδϡʔϧϕʔεͰ֦ுத


    • WASI ରԠ wasm ͳΒϒϥ΢β֎Ͱ΋࣮ߦՄೳɻݱࡏCͱRustͷΈରԠ
    10

    View Slide

  11. Rust
    11

    View Slide

  12. Rust
    • 2010-


    • Rust Foundation(2021-)


    • ύϑΥʔϚϯε΍৴པੑ(ϝϞϦ҆શੑ)Λॏࢹ


    • ςετ༻ߏจΛಉࠝ
    12

    View Slide

  13. Rust
    • جຊతʹʮݻ͘ʯॻ͘ݴޠ


    • ௿ϨΠϠʔΛ࿔ΔͨΊͷݴޠɺͱࢥͬͪΌͬͯྑ͍͔΋


    • จ๏ଟ͍
    13

    View Slide

  14. 14

    View Slide

  15. Χχʁ
    • ʮRustΛॻ͘ਓʯ= rustacean


    • crustacean ͕༝དྷ
    15

    View Slide

  16. Χχʁ
    • ඇެࣜϚείοτ ferris


    • ferrous ͕༝དྷɻḊ͸మʹͰ͖Δɻ


    • ެࣜυΩϡϝϯτʹ͕ͬͭΓొ৔͢Δ(ඇެࣜͱ͸)
    16

    View Slide

  17. Rustपลπʔϧ
    • rustup


    • cargo


    • clippy
    17

    View Slide

  18. Rustup
    • Rust ͷπʔϧνΣʔϯΠϯετʔϥʔ


    • Rust ͷΠϯετʔϧɺόʔδϣϯΞοϓͳͲ͕Ͱ͖Δ


    • Rust Λ࢖͏ͨΊʹ·ͣ࠷ॳʹΠϯετʔϧ͢Δπʔϧ
    18

    View Slide

  19. Cargo
    • Rust ͷύοέʔδϚωʔδϟʔ


    • ϓϩδΣΫτ࡞੒ɺґଘؔ܎؅ཧɺϏϧυɺςετ࣮ߦɺυΩϡϝϯ
    τੜ੒ͳͲ͍Ζ͍Ζ


    • rustupͰRustΛΠϯετʔϧ͢Δͱ෇͍ͯ͘Δ
    19

    View Slide

  20. Clippy
    • Rust ͷ Linter


    • ඞਢͰ͸ͳ͍͕͋Δͱ݁ߏศར
    20

    View Slide

  21. RustΤσΟγϣϯ
    • RustͷϦϦʔεαΠΫϧ͸6िؒ


    • 2~3೥͝ͱʹ৽͍͠ΤσΟγϣϯ͕࡞ΒΕΔ


    • Cargo.toml ͰϓϩδΣΫτͰར༻͢ΔΤσΟγϣϯΛઃఆ͢Δ


    • ݱࡏ(202211)͋ΔΤσΟγϣϯ͸2015, 2018, 2021
    21

    View Slide

  22. RustͷߏจΛͪΐͬͱ͚ͩ೷͍ͯΈΑ͏
    • Ownership


    • Pattern Matching


    • Trait
    22

    View Slide

  23. Ownership
    23

    View Slide

  24. Ownership ͱ͸
    • ஋͸ॴ༗ऀ͕࣋ͭ


    • ͋Δ஋ʹର͢Δॴ༗ऀ͸ඞͣҰ͚ͭͩ


    • ॴ༗ऀ͕είʔϓ͔Β֎ΕͨΒɺ஋͸ഁغ͞ΕΔ
    24

    View Slide

  25. Ownership ͷϞνϕʔγϣϯ
    • ϓϩάϥϜʹ͓͚ΔϝϞϦ؅ཧΛָʹݫີʹ΍Γ͍ͨ


    • ΨϕʔδίϨΫγϣϯ: ੍ָ͕ͩޚ͠ʹ͍͘


    • ໌ࣔతʹϝϞϦ֬อ/։์͢Δ: ݫີ͕ͩେม
    25

    View Slide

  26. Ownership
    { // a͸ɺ͜͜Ͱ͸༗ޮͰ͸ͳ͍ɻ·ͩએݴ͞Ε͍ͯͳ͍


    let a = 5; // a͸ɺ͔͜͜Β༗ޮʹͳΔ


    // aͰ࡞ۀΛ͢Δ


    } // ͜ͷείʔϓ͸ऴΘΓɻ΋͏a͸༗ޮͰ͸ͳ͍


    26

    View Slide

  27. Ownership(୅ೖ)
    {


    let x = 5;


    let y = x;


    println!("x = {}, y = {}", x, y);


    }
    27

    View Slide

  28. Ownership(୅ೖ)
    {


    let s1 = String::from("hello");


    let s2 = s1;


    println!("{}, world!", s1);


    }


    28

    View Slide

  29. Ownership(୅ೖ)
    • ઌͷίʔυ͸ಈ͔ͳ͍ʂ


    • จࣈྻ͸஋ͷϙΠϯλΛ࣋ͭ


    • ୅ೖ͢ΔͱϙΠϯλ͕ίϐʔ͞ΕΔ(shallow copy)


    • Rust Ͱ͸ɺs1 Λ s2 ʹ୅ೖͨ࣌͠఺Ͱจࣈྻͷॴ༗ऀ͕ s2 ʹҠΔ
    ˠmove
    29

    View Slide

  30. Ownership(୅ೖ)
    • ͋Εʁ੔਺ͷͱ͖͸ಈ͍͚ͨͲɾɾɾʁ


    • ੔਺ͷ৔߹ɺshallow copy ͱ deep copy ͷ۠ผ͕ແ͍(ཁΒͳ͍)


    • ۠ผ͕ཁΒͳ͍ܕͷྫ: u32, bool, char ͳͲ
    30

    View Slide

  31. Ownership(ؔ਺)
    • ؔ਺ʹ஋Λ౉ͨ͠Γɺؔ਺͔Β஋Λฦͨ͠Γͯ͠΋ɺϜʔϒ͞ΕΔ
    31

    View Slide

  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

    View Slide

  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

    View Slide

  34. Ownership(ؔ਺)
    • ؔ਺ʹ஋Λ౉͢ͱ͍͍ͪͪϜʔϒ͞ΕΔͷ໘౗...ɻ


    • →஋Ͱ͸ͳ͘஋ͷࢀরΛ౉͢͜ͱ͕Ͱ͖Δ


    • ؔ਺ͷҾ਺ʹࢀরΛऔΔ͜ͱ=आ༻ͱݺͿ
    34

    View Slide

  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

    View Slide

  36. Ownership(ࢀর)
    fn main() {


    let s = String::from("hello");


    change(&s);


    }


    // आ༻ͨ͠Կ͔Λมߋ͍ͨ͠ʂ


    fn change(some_string: &String) {


    some_string.push_str(", world");


    }
    36

    View Slide

  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

    View Slide

  38. Pattern Matching
    38

    View Slide

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

    View Slide

  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

    View Slide

  41. Pattern Matching
    • ύλʔϯ͸ॱʹॲཧ͞Εɺ࠷ॳʹҰகͨࣜ͠ͷ݁ՌΛฦ͢


    • Ұகͨ͠ΒҎ߱ͷΞʔϜ(ύλʔϯ => ࣜ)͸ॲཧ͞Εͳ͍(୹བྷධՁ)


    • matchࣜ͸໢ཏతͰͳ͚Ε͹ͳΒͳ͍


    • _ ͸ԿʹͰ΋Ϛον͢Δ
    41

    View Slide

  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

    View Slide

  43. Pattern Matching(Ψʔυ)
    • ͋Δ৚݅Λຬͨ͢΋ͷͷΈΛϚονͤ͞Δ͜ͱ͕Ͱ͖Δ(Ψʔυ)
    43

    View Slide

  44. Optionܕ
    • Rust ʹ͸ null ͕ͳ͍


    • ͨͩ͠ʮແޮͳ஋ʯΛද͢ํ๏͕༻ҙ͞Ε͍ͯΔ
    44

    View Slide

  45. Optionܕ
    enum Option {


    Some(T), // ༗ޮͳ஋


    None, // ແޮͳ஋


    }


    45

    View Slide

  46. Pattern Matching(෦෼ଋറ)
    fn plus_one(x: Option) -> Option {


    match x {


    None => None,


    Some(i) => Some(i + 1),


    }


    }


    let five = Some(5);


    let six = plus_one(five);


    let none = plus_one(None);


    46

    View Slide

  47. Pattern Matching(෦෼ଋറ)
    • ύλʔϯʹϚονͨ͠ʮ͋Δ෦෼ʯΛผͷม਺ʹଋറͰ͖Δ
    47

    View Slide

  48. Pattern Matching(ม਺)
    struct Point {


    x: i32,


    y: i32,


    }


    let ((hoge, fuga), Point {a, b}) = ((3, 10), Point { x: 3, y: -10 });
    48

    View Slide

  49. Pattern Matching
    • ม਺એݴͷͱ͖ʹ΋Pattern Matching ͕Ͱ͖Δ
    49

    View Slide

  50. Trait
    50

    View Slide

  51. Traitͱ͸
    • ʮಛఆͷৼΔ෣͍Λ࣋ͭܕʯΛఆٛͰ͖Δ


    • Java ͷΠϯλʔϑΣʔεɺSwift ͷϓϩτίϧʹ͍ۙ


    • ʮܕͷάϧʔϐϯάʯ͕Ϟνϕʔγϣϯ(ͨͿΜͶ)
    51

    View Slide

  52. Trait(ఆٛ)
    trait Purchasable {


    fn get_subtotal_price(&self) -> f64;


    }
    52

    View Slide

  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

    View Slide

  54. Trait
    • Trait Λ࣮૷͢Δܕશͯʹରͯ͠ɺશͯͷϝιουͷ࣮૷Λཁٻ͢Δͷ
    ͸େม


    • →σϑΥϧτ࣮૷ΛఆٛͰ͖Δ


    • ඞཁʹԠͯ͡ΦʔόʔϥΠυͰ͖Δ
    54

    View Slide

  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

    View Slide

  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

    View Slide

  57. Trait
    • ؔ਺ͷҾ਺΍ฦΓ஋ʹɺʮಛఆͷTraitΛ࣮૷͍ͯ͠ΔܕʯΛࢦఆ͢Δ
    ͜ͱ͕Ͱ͖Δ
    57

    View Slide

  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(arg: &T) -> f64 {


    let tax_included_price = arg.get_tax_included_price();


    let subtotal = arg.get_subtotal_price();


    tax_included_price - subtotal


    }
    58

    View Slide

  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

    View Slide

  60. ·ͱΊ
    • ஌Βͳ͍͏ͪʹ2021ΤσΟγϣϯ͕Ͱ͖͍ͯͯϏϏΓ·ͨ͠


    • ࠓ೔঺հͨ͠ߏจ͸ණࢁͷҰ֯Ͱ͢


    • ֶशۂઢ͕ٸफ़ͱݴΘΕΔ Rust ͷϋʔυϧ͕গ͠Ͱ΋Լ͕Ε͹޾͍
    Ͱ͢
    60

    View Slide

  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

    View Slide