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. 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
  2. WASI • "Web"Assembly ͸ Web ͚ͩͷ΋ͷʹ͠ͳͯ͘΋ྑ͍ͷͰ͸ʁ • →ଞͷ؀ڥͰ΋ಈ͔ͦ͏ • OSͱͷ΍ΓͱΓ=γεςϜίʔϧ͕ඞཁ

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

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

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

    println!("x = {}, y = {}", x, y); } 27
  6. 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
  7. 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
  8. 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
  9. Ownership(ࢀর) fn main() { let s = String::from("hello"); change(&s); }

    // आ༻ͨ͠Կ͔Λมߋ͍ͨ͠ʂ fn change(some_string: &String) { some_string.push_str(", world"); } 36
  10. 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
  11. 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
  12. 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
  13. 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
  14. Pattern Matching(ม਺) struct Point { x: i32, y: i32, }

    let ((hoge, fuga), Point {a, b}) = ((3, 10), Point { x: 3, y: -10 }); 48
  15. 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
  16. 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
  17. 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
  18. 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
  19. 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