Share
Embed
Rustで書いたツールのdebianパッケージングに挑戦してみた話(仮)
最近個人的に触りだしたRustというプログラミング言語について、
そもそもどういう言語なんだというご紹介(といっても、私も勉強中の身ですが)と、
Rustで書いたツールのdebを作ってみる、というところまでを目標としています。
Katsuki Kobayashi
March 24, 2019
Transcript
75 slides
-
Rust Ͱॻ͍ͨπʔϧͷ debian ύοέʔδϯάʹ ઓͯ͠Έͨ (Ծ) Katsuki Kobayashi ؔ Debian
ษڧձ 2019 3 ݄ 24 () -
ࣗݾհ ▶ Katsuki Kobayashi • ΈࠐΈΤϯδχΞ • ༻ݴޠ: C, Ξηϯϒϥ
(ARM) • Ӭԕʹษڧத: Python, Kotlin, Ξηϯϒϥ (RISC-V), Rust ▶ Debian ͱͷ͖͍ͭ͋ • େֶ࣌ʹૉఢͳઌഐ͕ͨ (͏ͪݱࡏ DD2 ໊,DM1 ໊) ʹΑͬͯڭ • ݸਓతʹύοέʔδͷߏͷ౷Ұײ͕Ұ൪͖ ▶ Rust • ৽͍͠ݴޠ͕ษڧ͍ͨ͠ • Ͱ Python Ruby ωΠςΟϒͳόΠφϦʹͳΒͳͯ͘πϥ͍ • ͡Ό͋ Mozilla ͖ͩ͠ Rust ʹ͠Α͏ -
ࣗݾհ ▶ Katsuki Kobayashi • ΈࠐΈΤϯδχΞ • ༻ݴޠ: C, Ξηϯϒϥ
(ARM) • Ӭԕʹษڧத: Python, Kotlin, Ξηϯϒϥ (RISC-V), Rust • ࠷ۙձࣾͰ C++Λษڧͤ͞ΒΕ͍ͯΔɻλεέςɻ ▶ Debian ͱͷ͖͍ͭ͋ • େֶ࣌ʹૉఢͳઌഐ͕ͨ (͏ͪݱࡏ DD2 ໊,DM1 ໊) ʹΑͬͯڭ • ݸਓతʹύοέʔδͷߏͷ౷Ұײ͕Ұ൪͖ • ͋Μ·Γߩݙ͍ͯ͠ͳ͍ ▶ Rust • ৽͍͠ݴޠ͕ษڧ͍ͨ͠ • Ͱ Python Ruby ωΠςΟϒͳόΠφϦʹͳΒͳͯ͘πϥ͍ • Go ͕ྲྀߦͬͯΔΒ͍͠ • ͡Ό͋ Mozilla ͖ͩ͠ Rust ʹ͠Α͏ ˡ? -
ຊͷྲྀΕ ▶ Rust Λ͞ΘͬͯΈΔ • rustup ͰπʔϧνΣʔϯΛΠϯετʔϧ • Cargo ͰϏϧυͯ͠ΈΔ
▶ Rust ͷಛͷ͝հ • ҙ: ൃදऀૉਓͳͷͰ͍࣭͠ճͰ͖·ͤΜ • https://doc.rust-lang.org/stable/book/ ΛಡΜͰ͍ͩ͘͞!! ▶ Debian ύοέʔδϯάΛࢼͯ͠Έͨ -
Rust ͱ? ▶ Mozilla ͕։ൃͨ͠γεςϜϓϩάϥϛϯάݴޠ • Servo(ઈࢍ։ൃத) ͱ͍͏ϒϥβΤϯδϯͷͨΊʹ։ൃ • ҆શੑɾฒߦੑʹ͍ͭͯߟ͑ΒΕͯઃܭ͞Ε͍ͯΔ
• ੩తܕ͚ݴޠ • ֶशۂઢ͕πϥΠͱ͏Θ͞ ▶ Χχ? • Rust ϓϩάϥϚʔͷࣄΛ Rustacean(ϥετγΞϯ) ͱݺͿ • “Crustacean(ߕ֪ྨ)“ ͔Βདྷ͍ͯΔΒ͍͠ − ΦϥΠϦʔຊͷදࢴΦΦώϩόΦΪΨχ − ඇެࣜͳϚείοτΧχ (Ferris ͍ͬͯ͏༷) ▶ Rust ͷϑΝΠϧͷ֦ுࢠ .rs • Rust Ͱॻ͔ΕͨϓϩδΣΫτͷެࣜαΠτ͕ http://xxx.rs Ͱ͋Δ͜ͱ͕ଟ͍ • ࠃผίʔυτοϓυϝΠϯ .rs : ηϧϏΞ -
Πϯετʔϧ ▶ rustup ΛೖΕΔͷ͕ҰԠެࣜ • curl https://sh.rustup.rs -sSf | sh
• ͋ͳͨͷ home dir ʹ৭ʑͱೖΓ·͢ ~/.cargo ▶ rustup ͷαϒίϚϯυͨͪ default σϑΥϧτͷπʔϧνΣʔϯΛΓସ͑·͢ɻ • πʔϧνΣʔϯ: stable ͔ nightly ͔ಛఆͷόʔδϣϯΛࢦఆͰ͖Δ update ߋ৽Λ͔͚·͢ɻnightly Λ͏߹ͪΐͪ͘ΐ͘͏ • ͦͯͨ͠·ʹյΕΔ • յΕΔͱ͍ͬͯɺίϯύΠϥ͕͓͔͘͠ͳͬͨࣄͳ͍Ͱ͢ • 10 લ͘Β͍ͷ sid ͷؾΛຯΘ͑·͢ completions γΣϧͷิ༻ͷίʔυΛు͘ (bash, zsh, fish, PowerShell, etc..) -
Debian ύοέʔδͰͷΠϯετʔϧ ▶ ͪΖΜ Debian ͷύοέʔδ͋Δ • ϏϧυπʔϧͰ͋Δ cargo ͷύοέʔδΛೖΕΔͷ͕ΑΖ͍͔͠ͱ
• ҰॹʹίϯύΠϥ (rustc) ೖΓ·͢ % apt show cargo Package: cargo Version: 0.33.0-1 Priority: optional Section: rust Maintainer: Rust Maintainers <[email protected]> Installed-Size: 9,453 kB Depends: libc6 (>= 2.18), libcurl3-gnutls (>= 7.28.0), libgcc1 (>= 1:4.2), \ libgit2-27 (>= 0.26.0), libssh2-1 (>= 1.2.5), libssl1.1 (>= 1.1.0), \ zlib1g (>= 1:1.1.4), rustc (>= 1.24), binutils, gcc | clang | c-compiler Suggests: cargo-doc, python3 Homepage: https://crates.io/ Download-Size: 2,483 kB APT-Manual-Installed: yes APT-Sources: http://ftp.jp.debian.org/debian sid/main amd64 Packages -
Hello World (1/4) ▶ Rust ͷϓϩδΣΫτɺ cargo new Ͱ࡞Γ·͢ •
--bin (ݱ default) ͔ --lib (چ default) ͰύοέʔδͷछྨࢦఆͰ͖·͢ % cargo new hello Created binary (application) ‘hello‘ package ▶ ࣮ߦ͢ΔͱɺCargo.toml ͱ src/main.rs ͕Ͱ͖Δ % tree . ᵓᴷᴷ Cargo.toml ᵋᴷᴷ src ᵋᴷᴷ main.rs 1 directory, 2 files -
Hello World (2/4) ▶ ࣮ϓϩδΣΫτΛ࡞ͬͨ࣌Ͱ Hello World ͷ͕͍ྃͯ͠Δ • ੜ͞Εͨ
src/main.rs ͷதˣ 1 fn main() { 2 println!("Hello,␣world!"); 3 } ▶ ࣮ߦ͢Δʹ cargo run • ࣮σόοάϏϧυͳͷͰɺ࠷దԽΛ͔͚Δʹ--release Λ͚ͭΔ % cargo run Compiling hello v0.1.0 (/path/to/hello) Finished dev [unoptimized + debuginfo] target(s) in 0.31s Running ‘target/debug/hello‘ Hello, world! -
Hello World (3/4) ▶ গ͠ίʔυΛݟͯΈΔ 1 fn main() { 2
println!("Hello,␣world!"); 3 } ؔఆٛ fn Λ͏ main ؔ Γॻ͔ͳ͍ println!() ࣮ؔͰͳͯ͘ϚΫϩ (!͕͍ͯΔͷϚΫϩ) -
Hello World (4/4) ▶ Cargo.toml [package] name = "hello" version
= "0.1.0" authors = ["Katsuki Kobayashi <[email protected]>"] edition = "2018" [dependencies] ▶ Cargo.lock (Ϗϧυ͢Δͱࣗಈੜ) # This file is automatically @generated by Cargo. # It is not intended for manual editing. [[package]] name = "hello" version = "0.1.0" -
ͦΕ͡Ό͋ʜʜ ▶ ͪΐͬͱ৭ʑͱ Rust ͷߏจͱ͔ػೳΛհ͠·͢ • ղΒͳ͍ or ͬͱৄ͘͠ͱࢥͬͨΒ The
book ΛಡΜͰΒ͑Εͱ − ޱ಄Ͱฉ͍ͯΒͬͯྑ͍Ͱ͕͢ճʹځ͢ΔՄೳੑ͕ߴ͍Ͱ͢ ;-) • The book: https://doc.rust-lang.org/stable/book/ • ࣮ߦڥ web ͰͬͯΒͬͯྑ͍͔Ͱ͢ − https://play.integer32.com/ ▶ ·͡Ίʹ͖߹͏ʹ݁ߏେม • ΧχຊͰҾ༻͞ΕͯΔ Quora ͷهࣄ • C++ͷຊΛݱࡏਐߦܗͰಡΜͰΔʹ͘͢͝ೲಘ − େମɺC++11 Ͱ͜͏ॻ͚Δ͔ΒͦΕ͕ਪ͍ͬͯ͏ͷ͕ Rust ͷจ๏ I’ve found that Rust has forced me to learn many of the things that I was slowly learning as "good practise" in C/C++ before I could even compile my code. -
มએݴ (1/3) 1 let x = 1; 2 println!("x␣=␣{}", x);
3 let x = 1.25; 4 println!("x␣=␣{}", x); 5 // x = 1; // error: expected floating-point number, 6 // found integer 7 // x = 1.5; // error: cannot assign twice to 8 // immutable variable ▶ มͷએݴ let Ͱߦͳ͏ • ܕ (ҰҙʹਪͰ͖ΔͳΒ) লུՄೳ • γϟυʔΠϯάՄೳ • ࢦఆ͕ͳ͚Ε immutable ͳมʹͳΔ -
มએݴ (2/3) 1 let mut y: u32 = 1; 2
y -= 1; 3 y -= 1; 4 println!("y␣=␣{}", y); ▶ มͷએݴ let Ͱߦͳ͏ • ܕޙஔه๏ (ίϩϯͷޙΖʹ͚ͭΔ) • mutable ͳมʹ͚ͨ͠Ε mut ΩʔϫʔυΛ͏ ▶ ͪͳΈʹɺ࣮ߦ͢ΔͱσόοάϏϧυͩͱ్தͰࢭ·Γ·͢ % cargo run Compiling variable v0.1.0 (/path/to/examples/variable) Finished dev [unoptimized + debuginfo] target(s) in 0.00s Running ‘target/debug/variable‘ thread ’main’ panicked at ’attempt to subtract with overflow’, \ variable/src/main.rs:12:5 note: Run with ‘RUST_BACKTRACE=1‘ environment variable to \ display a backtrace. -
มએݴ (3/3) ▶ ϦϦʔεϏϧυ (--release ͖) ͢Δͱ࠷ޙ·Ͱ࣮ߦ͢Δ • ͪΖΜɺ݁Ռ͓͔͠ͳ͜ͱʹͳΔ %
cargo run Compiling variable v0.1.0 (/path/to/examples/variable) Finished release [optimized] target(s) in 0.30s Running ‘target/release/variable‘ x = 1 x = 1.25 y = 4294967295 -
جຊతͳܕ ූ߸ແ͠: u8, u16, u32, u64, usize ූ߸͖: i8,
i16, i32, i64, isize ුಈখ ୯ਫ਼: f32ɺഒਫ਼: f64 ϒʔϧ bool จࣈ char (Unicode ͷ 1 จࣈ) จࣈྻ String ▶ ͨͩ͠ɺจࣈྻϦςϥϧͷ str ͕͋ͬͯͱ͍ͯ͜͠ ྻ [T; N] (T: ܕ, N: ཁૉ) ϕΫλʔ Vec<T> (T: ܕ) εϥΠε &[T] (T: ܕ) -
String ͱ str (1/2) ▶ C++ͷ std::string ͱ char ʹࣅ͍ͯΔ
1 let a = "abcd".to_string(); // String::from("abcd"); Մ 2 let b = &a[1..]; 3 let c = "zyxwv"; -
String ͱ str (2/2) ▶ String Մม 1 let mut
a = "abcd".to_string(); 2 // ^^^ 3 a.push_str("efg"); // OK 4 5 let mut c = "zyxwv"; 6 // no method named ‘push_str‘ found 7 // for type ‘&str‘ in the current scope 8 c.push_str("ut"); // Error!! -
ؔ ▶ fn Ωʔϫʔυͱ -> τʔΫϯΛͬͯॻ͘ 1 fn add(a: i32,
b: i32) -> i32 { 2 return a + b; 3 } 4 5 fn main() { 6 println!("{}", add(1, 2)); 7 } % cargo run Compiling function v0.1.0 (/path/to/examples/function) Finished dev [unoptimized + debuginfo] target(s) in 0.20s Running ‘target/debug/function‘ 3 -
ॴ༗ݖ (1/4) ▶ ܕͱจࣈྻܕͷมΛෳͷมʹೖͯ͠ΈΔ 1 let i0 = 1; 2
let i1 = i0; 3 let i2 = i0; 4 5 let s0: String = "hoge".to_string(); 6 let s1 = s0; 7 let s2 = s0; -
ॴ༗ݖ (1/4) ▶ ܕͱจࣈྻܕͷมΛෳͷมʹೖͯ͠ΈΔ • Ͳ͏ͳΔͱࢥ͍·͢? 1 let i0 =
1; 2 let i1 = i0; 3 let i2 = i0; 4 5 let s0: String = "hoge".to_string(); 6 let s1 = s0; 7 let s2 = s0; -
ॴ༗ݖ (2/4) ▶ จࣈྻܕͷํ͚ͩίϯύΠϧΤϥʔʹͳΔ • Rust ͰɺೖɺؔͷҾɺؔͷΓͰॴ༗ݖ͕Ҡಈ͢Δ • ͨͩ͠ɺɺҰ෦ͷܕίϐʔʹͳΔ −
ͦͷͨΊ i2 ͰΤϥʔʹͳ͍ͬͯͳ͍ − ίϐʔ͞ΕΔܕʹ͍ͭͯɺΤϥʔͰग़͍ͯΔ “Copy trait ” ͱ͍͏ͷ͕ϛι error[E0382]: use of moved value: ‘s0‘ --> ownership/src/main.rs:8:15 | 6 | let s0: String = "hoge".to_string(); | -- move occurs because ‘s0‘ has type ‘std::string::String‘, \ which does not implement the ‘Copy‘ trait 7 | let s1 = s0; | -- value moved here 8 | let s2 = s0; | ^^ value used here after move -
ॴ༗ݖ (3/4) ▶ ؔͷҾବͳͷͰҎԼΞτ 1 fn consume(_s: String) {} 2
// தུ 3 let h: String = "Hello␣World".to_string(); 4 consume(h); 5 let hh = h; 12 | let h: String = "Hello World".to_string(); | - move occurs because ‘h‘ has type ‘std::string::String‘, \ which does not implement the ‘Copy‘ trait 13 | consume(h); | - value moved here 14 | let hh = h; | ^ value used here after move -
ॴ༗ݖ (4/4) ▶ ٯʹؔͷΓͷॴ༗ݖΛݺͼݩʹͤΔ 1 fn generate(i: i32) -> Vec<i32>
{ 2 let mut v = Vec::new(); 3 v.push(i); 4 return v; 5 } 6 // ུ 7 let v1 = generate(10); 8 let mut v2 = generate(100); 9 v2.push(101); 10 println!("v1:␣{:?}", v1); // v1: [10] 11 println!("v2:␣{:?}", v2); // v2: [100, 101] -
ࢀর, आ༻ (1/2) ▶ ॴ༗ݖΛͣ͞ʹΛ͏ʹࢀরΛ͏ • Rust Ͱʮआ༻ʯͱݴ͏ • C++ͱ͓ͳ͘͡ɺ
“&” Λ͏ • C++ͱҧ͍ − आ༻͢Δଆ (ࢀর͞ΕΔଆ) ʹ “&” Λ͚Δ − ؔͷݺͼग़͠ݩͱݺͼग़͠ઌ྆ํʹ “&” ͕ඞཁ 1 fn consume(_s: &String) {} 2 // ུ 3 let s0: String = "hoge".to_string(); 4 let s1 = &s0; 5 let s2 = &s0; 6 7 let h: String = "Hello␣World".to_string(); 8 consume(&h); 9 let hh = h; -
ࢀর, आ༻ (2/2) ▶ ࢀরͷղܾʹ “*” Λ͏ 1 let i0
= 5; 2 let i1 = &i0; 3 4 println!("i0:␣{},␣i1:␣{}", i0, *i1); ▶ ͰɺؔͰ͏߹ͩͬͨΓɺ৭ʑͳ໘ͰলུͰ͖ͨΓ͢Δ 1 fn catlength(a: &String, b: &String) -> usize { 2 return a.len() + b.len(); 3 } 4 // ུ 5 let s0 = "hoge".to_string(); 6 let s1 = "fuga".to_string(); 7 println!("{}", catlength(&s0, &s1)); -
ϥΠϑλΠϜ (1/6) ▶ C ݴޠͱ͓ͳ͘͡ Rust ͷมʹείʔϓ͕͋Δ (GC ͱ͔ͳ͍) 1
{ 2 let a = 0; 3 } // ͜͜Ͱυϩοϓ 4 println!("{}", a); error[E0425]: cannot find value ‘a‘ in this scope --> lifetime/src/main.rs:5:20 | 5 | println!("{}", a); | ^ not found in this scope -
ϥΠϑλΠϜ (2/6) ▶ ࢀর͍ͯͨ͠߹ɺίϯύΠϧ࣌ʹౖΒΕΔ • C/C++ͰΑ͘Δղ์ࡁΈͷϝϞϦΞΫηεͱ͔͕ίϯύΠϧ࣌ʹൃݟͰ͖Δ!! 1 let r; 2
{ 3 let b = 0; 4 r = &b; 5 } 6 println!("{}", r); error[E0597]: ‘b‘ does not live long enough --> lifetime/src/main.rs:11:9 | 11 | r = &b; | ^^^^^^ borrowed value does not live long enough 12 | } | - ‘b‘ dropped here while still borrowed 13 | println!("{}", r); | - borrow later used here -
ϥΠϑλΠϜ (3/6) ▶ ͞Βʹ͍͜͠ͷ͕ؔͰɺҎԼΤϥʔʹͳΔ • ͞ΒΓͱॻ͍ͯ·͕͢ɺηϛίϩϯΛൈ͔͢ͱΛ࡞Γ·͢ − ͦͷͨΊɺ x
y ͷޙΖʹηϛίϩϯ͕ͳ͍ − if ࣜ ͷޙΖʹηϛίϩϯ͕ͳ͍ 1 fn longest(x: &str, y: &str) -> &str { 2 if x.len() > y.len() { 3 x 4 } else { 5 y 6 } 7 } -
ϥΠϑλΠϜ (4/6) 1 fn longest(x: &str, y: &str) -> &str
{ 2 if x.len() > y.len() { 3 x 4 } else { 5 y 6 } 7 } error[E0106]: missing lifetime specifier --> lifetime/src/main.rs:1:33 | 1 | fn longest(x: &str, y: &str) -> &str { | ^ expected lifetime parameter | = help: this function’s return type contains a borrowed value, \ but the signature does not say whether it is borrowed from ‘x‘ or ‘y‘ -
ϥΠϑλΠϜ (5/6) error[E0106]: missing lifetime specifier --> lifetime/src/main.rs:1:33 | 1
| fn longest(x: &str, y: &str) -> &str { | ^ expected lifetime parameter | = help: this function’s return type contains a borrowed value, \ but the signature does not say whether it is borrowed from ‘x‘ or ‘y‘ ▶ ͳʹ͔? • ࣮ߦ͢Δ·Ͱ x ͱ y (ͲͪΒࢀর) ͷͲͪΒΛฦ͔͢Θ͔Βͳ͍ • ΓͷϥΠϑλΠϜ͕Ͳ͏ͳΔ͔Θ͔Βͳ͍ͷͰɺͦͷޙͷॲཧ͕νΣοΫͰ͖ͳ͍ ▶ Μ͡ΌͲ͏͢Δͷ? • ίϯύΠϥౖ͕͍ͬͯΔ௨Γɺlifetime parameter Λॻ͚ OK • جຊతʹ Rust ͷίϯύΠϥͷΤϥʔϝοηʔδͰ͢ − Ұ෦ͷίϯύΠϥ͕ΧΦεͳ͚ͩ͠Ε·ͤΜ͕ʜʜͶ͐ɺgcc ͞Μ -
ϥΠϑλΠϜ (6/6) ▶ ҎԼͷΑ͏ʹॻ͘ඞཁ͕͋Δ • ੜଘظؒΛ ’a ͱ͢Δ − ه߸׳ྫతʹదͳ༷
• Γ x, y ͷ͍ํͷੜଘظؒΛ࣋ͭඞཁ͕͋Δ • fn longest<’a, ’b>(x: &’a str, y: &’b str) -> &’a str ͱॻ͚Δ͕ y ͷੜଘظ͕ؒ ’a Ͱͳ͍ͨΊΤϥʔʹͳΔ 1 fn longest<’a>(x: &’a str, y: &’a str) -> &’a str { 2 if x.len() > y.len() { 3 x 4 } else { 5 y 6 } 7 } -
ߏମ (1/2) ▶ Rust ʹ class ͳ͍ • ૉʹ (?)
ߏମΛ͏ • ͷએݴ֤࣌ཁૉͷ໊લ͕ඞཁ (·ͬͨ͘ಉ໊ͷมͰॳظԽ͢Δͱ͖ͷΈলུՄ) • ຊѻ͍·ͤΜ͕ɺϥΠϑλΠϜབྷΜͰ͘Δ 1 struct Point { 2 x: f64, 3 y: f64, 4 } 5 // ུ 6 let _p0 = Point { x: 1.0, y: 1.0 }; -
ߏମ (2/2) ▶ ߏମ͚ͩͲɺؔΛੜͤΔΑ ww • ࠷ॳͷҾ self(Λมߋ͍ͨ͠߹ mut Λ͚Δ)
1 struct Point { 2 x: f64, 3 y: f64, 4 } 5 6 impl Point { 7 fn abs(&self) -> f64 { 8 (self.x * self.x + self.y * self.y).sqrt() 9 } 10 } 11 12 fn main() { 13 let p0 = Point { x: 1.0, y: 1.0 }; 14 println!("{}", p0.abs()); // => 1.4142135623730951 15 } -
δΣωϦοΫ ▶ ߏମɺؔΛ generic ʹ͢Δ͜ͱ͕Ͱ͖Δ • C++ͷςϯϓϨʔτɺJava ͷδΣωϦΫεతͳͷ 1 struct
GenPoint<T> { 2 x: T, 3 y: T, 4 } 5 // ུ 6 let _p1 = GenPoint::<i32> { x: 1, y: 1 }; 7 let _p2 = GenPoint { x: 1, y: 1 }; 8 let _p3 = GenPoint { x: 1.5, y: 2.0 }; ▶ GenPoint<T, U> ͱͯ͠ɺ y Λ U ͱ͢Δ͜ͱͰ͖Δ -
Trait (1/5) ▶ Java ͱ͔ Go ͷΠϯλʔϑΣʔεతͳͷ͋Δ -> Trait(τϨΠτ) •
͖͞΄Ͳग़͖ͯͨ Copy trait ίϨ ▶ trait ͷఆٛҎԼͷΑ͏ʹ trait ΩʔϫʔυͰ࡞Δ • duck walk ͯ͠ quack Ͱ͖Δͷ 1 trait Duck { 2 fn walk(&self); 3 fn quack(&self); 4 } -
Trait (2/5) ▶ ࣮ҎԼͷΑ͏ʹ 1 struct RealDuck {} 2 3
impl Duck for RealDuck { 4 fn walk(&self) { 5 println!("duck␣walking"); 6 } 7 8 fn quack(&self) { 9 println!("quack") 10 } 11 } -
Trait (3/5) ▶ ಉ༷ʹ Dog ࣮͢Δ 1 struct Dog {}
2 3 impl Duck for Dog { 4 fn walk(&self) { 5 println!("dog␣walking"); 6 } 7 8 fn quack(&self) { 9 println!("bow") 10 } 11 } -
Trait (4/5) ▶ ͦ͏͢ΔͱɺҎԼͷΑ͏ʹ Duck ΛҾʹͨؔ͠ test_duck() ʹ RealDuck ͱ
Dog ͷͲͪΒ͢͜ͱ͕Ͱ͖Δ 1 fn test_duck(duck: &Duck) { 2 duck.walk(); 3 duck.quack(); 4 } 5 6 fn main() { 7 let duck = RealDuck {}; 8 let dog = Dog {}; 9 10 test_duck(&duck); 11 test_duck(&dog); 12 } -
Trait (5/5) ▶ ෳͷ Trait Λ࣮ͨ͠ܕΛཁٻ͢Δ͜ͱͰ͖Δ • pub fn notify(item:
impl Summary + Display) { − Summary ͱ Display Λಉ࣌ʹ࣮ͨ͠ܕ ▶ ಡΈਏ͍ͷͰɺҎԼͷॻࣜ͋Δ • pub fn notify<T: Summary + Display>(item: T) { ▶ ͞Βʹ͜Μͳॻࣜʜʜ 1 fn some_function<T, U>(t: T, u: U) -> i32 2 where T: Display + Clone, 3 U: Clone + Debug 4 { -
enum (1/5) ▶ Rust ͷ enum ৭ʑͱ͓͔͍͠ ૬ʹڧྗ ▶
C ݴޠͷΑ͏ͳ enum ͪΖΜ࡞ΕΔ 1 enum Fruits { 2 Apple, 3 Banana, 4 Orange, 5 Peach, 6 } 7 // ུ 8 let _hoge = Fruits::Apple; -
enum (2/5) ▶ enum ͷதʹΛ࣋ͭ͜ͱ͕Ͱ͖Δ • ͔͠ɺܕݸࣗ༝ࣗࡏ • (u8, u8,
u8, u8) λϓϧ 1 enum IpAddr { 2 V4((u8, u8, u8, u8)), 3 V6(String), 4 } 5 // ུ 6 let v4_addr = IpAddr::V4((127, 0, 0, 1)); 7 let v6_addr = IpAddr::V6("::1".into()); ▶ ͳ͓ɺenum ʹϝιουΛੜͤ·͢ www • struct ͷͱ͖ͱಉ͘͡ɺimpl ΩʔϫʔυΛ͏ -
enum (3/5) ▶ Λ࣋ͨͤͨ enum match ΛͬͯऔΓग़͢͜ͱ͕Ͱ͖Δ • C
ݴޠͷ switch จͷ default తͳͷ “_ =>” ͱॻ͘ 1 fn extract_addr(addr: &IpAddr) { 2 match addr { 3 IpAddr::V4(a) => { 4 println!("{}.{}.{}.{}", a.0, a.1, a.2, a.3); 5 } 6 IpAddr::V6(a) => { 7 println!("{}", a); 8 } 9 } 10 } 11 // ུ 12 extract_addr(&v4_addr); // => 127.0.0.1 13 extract_addr(&v6_addr); // => ::1 -
enum (4/5) ▶ λϓϧܕϰΝϦΞϯτɺߏମܕϰΝϦΞϯτͳΜͯͷʜʜ • Γɺmatch ΛͬͯऔΓग़͢ ▶ λϓϧܕϰΝϦΞϯτͷྫ 1
enum RoughTime { 2 InThePast(TimeUnit, u32), 3 JustNow, 4 InTheFuture(TimeUnit, u32), 5 } 6 // ུ 7 match &t { 8 RoughTime::InTheFuture(_, t) => { 9 println!("{}", t); 10 } -
enum (5/5) ▶ ߏମܕϰΝϦΞϯτͷྫ 1 enum Shape { 2 Sphere
{ center: Point3d, radius: f32 }, 3 Cuboid { corner1: Point3d, corner2: Point3d }, 4 } 5 // ུ 6 match &u { 7 Shape::Cuboid { 8 corner1: c1, 9 corner2: c2, 10 } => { 11 println!("{:?}", c1); 12 } ▶ ศར͕ͩɺΓ͗͢ΔͱܕͷαΠζ͕ແବʹେ͖͘ͳΔ • ࠷େαΠζͷϰΝϦΞϯτʹ߹Θ͞ΔͨΊ • େ͖ͳϰΝϦΞϯτΛώʔϓʹஔ͚ΔΑ͏ʹ͢Δ Box ܕΛ༻͍Δ • Box ܕʹ͍ͭͯຊѻ͍·ͤΜ!! -
Option<T>ͱ Result<T,E> (1/2) ▶ Rust ͷඪ४తͳܕ ▶ ఆٛҎԼͷΑ͏ʹͳ͍ͬͯΔ 1 pub
enum Option<T> { 2 None, 3 Some(T), 4 } 5 6 pub enum Result<T, E> { 7 Ok(T), 8 Err(E), 9 } ▶ Option<T> • ͕ͳ͍Մೳੑ͕͋ΔࣄΛࣔ͢ (nullptr ͷΘΓ) ▶ Result<T,E> • ࣦഊ͢ΔՄೳੑ͕͋ΔࣄΛࣔ͢ (ྫ֎ͷΘΓ) -
Option<T>ͱ Result<T,E> (2/2) ▶ ҎԼͷΑ͏ͳײ͡ͰΤϥʔఆ͢Δ • ৭ʑͱศརͳؔԋࢉࢠ͕͋Γ·͢. (?ԋࢉࢠ, expect(), unwrap())
1 let i = match env::args().nth(1) { 2 None => { 3 panic!("no␣arguments"); 4 } 5 Some(arg) => match arg.parse::<i32>() { 6 Ok(fig) => fig, 7 Err(e) => { 8 panic!("error:␣{}", e); 9 } 10 }, 11 }; 12 13 println!("i:␣{}", i); -
Crate (1/3) ▶ Rust ͷϓϩάϥϜΫϨʔτͱݺΕΔ୯ҐͷΈ߹ΘͤͰߏ͞ΕΔ • ৄࡉʹ͍ͭͯຊলུ͢Δͱͯ͠ʜʜ ▶ crates.io •
The Rust community’s crate registry ▶ ͍ํ • Cargo.toml ͷ [dependencies] ʹɺcrates.io ʹॻ͔Ε͍ͯΔࣜΛೖΕΕ OK • ͜͜ʹ git ͷϦϙδτϦΛͭͬ͜ΉࣄͰ͖Δ ▶ ͜͜ͰɺΦϓγϣϯղੳͷ crate ͷ clap ΛͬͨྫΛհ͠·͢ • -n Φϓγϣϯ͚ͩ͋Δ echo ίϚϯυͲ͖ [dependencies] clap = "2.32.0" -
Crate (2/3) ▶ Rust 2018 ͷલ·Ͱɺ extern crate clap; ͱ͢Δඞཁ͕͋ͬͨ
• Cargo.toml ͱ͔ͩͬͨΒ (?) ෆཁʹ 1 use clap::{App, Arg}; 2 3 fn main() { 4 let m = App::new("echo") // ίϚϯυ໊ 5 .arg(Arg::with_name("STRING").multiple(true)) // ෳͷҾ 6 .arg( // ’-n’ Λఆٛ 7 Arg::with_name("n") 8 .short("n") 9 .help("do␣not␣output␣the␣trailing␣newline"), 10 ) 11 .get_matches(); // ࣮ߦ 12 // ଓ͘ -
Crate (3/3) 1 // ଓ͖ 2 let out = match
m.values_of("STRING") { 3 // ˣຯʹΠςϨʔλΛ༻ 4 Some(v) => v.collect::<Vec<&str>>().join("␣"), 5 None => "".into(), 6 }; 7 8 print!("{}", out); 9 10 if !m.is_present("n") { 11 println!(""); // ’-nΦϓγϣϯ͕ͳ͚Εվߦ’ 12 } 13 } -
จ๏ͱ͔ʹ͍ͭͯҎ্!! ▶ ຊ·͓ͬͨ͑͘Ͱ͖ͳ͔ͬͨ༰ • Ϋϩʔδϟʔ • ΠςϨʔλʔ • εϚʔτϙΠϯλ •
ςετ − Rust ϓϩάϥϜͱಉ͡ϑΝΠϧʹςετΛॻ͍ͨΓɺίϝϯτͷαϯϓϧϓϩάϥϜͷςετ ͱ͔Ͱ͖Δ ▶ ڵຯ͕͋Εɺ The Book Λ͓ಡΈ͍ͩ͘͞ ▶ ࠶ܝ: Ϗϧυ͢Δͱ Cargo.lock ͱ͍͏ϑΝΠϧ͕ੜ͞ΕΔ • Cargo.toml ͬ͘͟Γͱͨ͠ґଘΛखಈͰ࡞ − όʔδϣϯ൪߸ϝδϟʔόʔδϣϯ͚ͩهड़Ͱ͖Δ • Cargo.lock શͳόʔδϣϯϋογϡΛ࣋ͬͯ Cargo ͕Ϗϧυ࣌ʹࣗಈͰ࡞ − Ξοϓσʔτ͚ͨ͠Ε cargo update Λ࣮ߦ -
Debian ύοέʔδʹͯ͠ΈΔ ▶ Ұ൪؆୯ͳํ๏ : cargo-deb • A cargo subcommand
that generates Debian packages from information in Cargo.toml − GitHub − Ξφϯε@users.rustlang.org • Πϯετʔϧํ๏ − cargo install cargo-deb : cargo ͷαϒίϚϯυͱͯ͠ೖΔ − deb ʜʜͳ͍ʜʜ!! • − ެࣜʹͱͯೖΒͳ͍ deb Λు͘ − ୯ҰόΠφϦͷϓϩδΣΫτʹ͔͠ରԠ͍ͯ͠ͳ͍ − ͔ͩΒ deb ʹͳͬͱΒΜͷ͔ʜʜ • ·͊ɺ͍͑ͱ/usr/bin ʹೖΕ͍ͨͳΒΞϦ͔Ͱ͕͢ʜʜ -
ਅ໘ʹ Debian ύοέʔδʹͯ͠ΈΔ ▶ Debian Rust packaging team • https://wiki.debian.org/Teams/RustPackaging
▶ crates.io ʹ͋Δͷʹ͍ͭͯ debcargo ͕͍͍ײ͡ʹͬͯ͘ΕΔΒ͍͠ • ࣮ࡍʹ debcargo package ripgrep Λ͢Δͱ ripgrep ͷ deb ͕࡞ΒΕΔ • ͱ͜Ζ͕ɺ͜ͷ debcargo ͞Μɺ crates.io ʹΞοϓ͞Ε͍ͯΔ͔͠ deb ʹͯ͘͠Εͳ͍༷ʜʜ ▶ ͜ΕҎ߱Ͱɺઌఔͷ clap ͷαϯϓϧͷ deb ࡞ʹઓ͠·͢ • ํͱͯ͠ɺdebcargo Ͱ͏ dh-cargo Λ͏ • https://github.com/rarewin/clap-test ʹιʔεஔ͍ͯ·͢ -
dh_make ͢Δ % dh_make -s -p rust-clap-test_0.1.0 --createorig Maintainer Name
: Katsuki Kobayashi Email-Address : [email protected] Date : Sat, 23 Mar 2019 18:03:48 +0900 Package Name : rust-clap-test Version : 0.1.0 License : blank Package Type : single Are the details correct? [Y/n/q] Currently there is not top level Makefile. This may require additional tuning Done. Please edit the files in the debian/ subdirectory now. ▶ debcargo Λ͏ͱɺύοέʔδ໊ʹ rust- ͘͠ librust- ͕ suffix ͱ༷ͯ͘͠ -
dh-cargo Λ͏Α͏ʹ͢Δ ▶ ͻͱ·ͣҎԼΛͯ͠ΈΔ • debian/rules ʹΦϓγϣϯΛՃ • debian/control ͷ
Build-Depends ʹ dh-cargo/librust-clap-dev ΛೖΕΔ %: dh $@ --buildsystem cargo ▶ ͱΓ͋͑ͣ debuild-pbuilder ͯ͠ΈΔ % debuild-pbuilder -> Attempting to satisfy build-dependencies -> Creating pbuilder-satisfydepends-dummy package Package: pbuilder-satisfydepends-dummy Version: 0.invalid.0 ... (snip) ... dh_autoreconf -O--buildsystem=cargo dh_auto_configure -O--buildsystem=cargo cp: ’./debian/cargo-checksum.json’ Λ stat Ͱ͖·ͤΜ: \ ͦͷΑ͏ͳϑΝΠϧσΟϨΫτϦ͋Γ·ͤΜ -
cargo-checksum.json? ▶ RustPackagingTeam • upstream ͷ crate ͷνΣοΫαϜΛؚΊΔ • /usr/share/cargo/registry
Λ͏ͨΊʹ͏ − deb ͷϏϧυʹωοτϫʔΫͬͪΌବ͔ͩΒͱࢥΘΕ • ˢͷΈ cargo-vendor Λར༻͍ͯ͠Δ − cargo-vendor ϩʔΧϧͷιʔεΛ cargo ͕֎෦ crate ͱͯ͠ѻ͏ͨΊͷΈΛఏڙ ▶ ͱ͔ɺΘΓͱෆʹॻ͍ͯ͋Δ (࡞Γํͷղઆͱ͔ͳ͍) ΜͰ͕͢ʜʜ -
cargo-checksum.json? ▶ RustPackagingTeam • upstream ͷ crate ͷνΣοΫαϜΛؚΊΔ • /usr/share/cargo/registry
Λ͏ͨΊʹ͏ − deb ͷϏϧυʹωοτϫʔΫͬͪΌବ͔ͩΒͱࢥΘΕ • ˢͷΈ cargo-vendor Λར༻͍ͯ͠Δ − cargo-vendor ϩʔΧϧͷιʔεΛ cargo ͕֎෦ crate ͱͯ͠ѻ͏ͨΊͷΈΛఏڙ ▶ ͱ͔ɺΘΓͱෆʹॻ͍ͯ͋Δ (࡞Γํͷղઆͱ͔ͳ͍) ΜͰ͕͢ʜʜ % apt source ripgrep % cat rust-ripgrep-0.10.0/debian/cargo-checksum.json {"package":"Could not get crate checksum","files":{}} ▶ ͍ͬͨΜΘ͢Ε·͢ • ͳΜͱɺtouch debian/cargo-checksum.json ͰϏϧυ͕ਐΉ • ͜ͷ “could not get crate checksum” debcargo ͷΤϥʔΈ͍ͨͰ͢ -
ґଘࠈͱҋͱ (1/4) ▶ Ϗϧυ͕ਐΉΑ͏ʹͳͬͨΑ͍͕ debian cargo wrapper: running subprocess ([’env’,
’RUST_BACKTRACE=1’, \ ’/usr/bin/cargo’, ’-Zavoid-dev-deps’, ’build’, ’--verbose’, ’--verbose’, \ ’-j4’, ’--target’, ’x86_64-unknown-linux-gnu’],) {} error: failed to select a version for the requirement \ ‘textwrap = "= 0.10.0"‘ candidate versions found which didn’t match: 0.11.0 location searched: directory source ‘/path/to/clap-test/debian/cargo_registry‘ \ (which is replacing registry ‘https://github.com/rust-lang/crates.io-index‘) required by package ‘clap v2.32.0‘ ▶ Կނ͔ textwrap ͷόʔδϣϯͰࣦഊʜʜ • clap 0.10.0 ͷ textwrap Λཁٻ • ݱࡏͷ debian sid/buster ͷ librust-textwrap-dev 0.11.0 -
ґଘࠈͱҋͱ (2/4) ▶ ͔͠͠ͳͥ? • ripgrep clap ʹґଘ͍ͯ͠ΔͷʹϏϧυͰ͖͍ͯΔ ▶
github ͷ ripgrep ͷ tar ͱ deb ͷ ripgrep ͷιʔεΛൺֱͯ͠ΈΔ • ඍົʹ͕ࠩ͋Δʜʜ • Ͳ͏Βɺcrates.io ͔Β࣋ͬͯ͘ΔͱɺCargo.toml ͕ฤू͞Ε (Cargo.toml ʹίϝϯτ ͋Γ)ɺCargo.lock ͕আ͞ΕΔ༷ (ͬͪ͋͜;) % grep textwrap Cargo.lock "textwrap 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)", name = "textwrap" "checksum textwrap 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "307686869c93e71f94da64286f9a9524c0f308a9e1c87a583de8e9c9039ad3f6" ▶ ͏ʔΉʜʜ -
ґଘࠈͱҋͱ (3/4) % rm Cargo.lock % debuild-pbuilder ... (snip) ...
Compiling textwrap v0.11.0 Running ‘rustc --crate-name textwrap.... ... (snip) ... Finished running lintian. ▶ ͳΜͱɺCargo.lock Λফͨ͠ΒϏϧυͰ͖ͯ͠·ͬͨʜʜ • debian/cargo_registry ҎԼʹґଘ crate ͷϩʔΧϧ % ls -lh debian/cargo_registry/textwrap-* ... debian/cargo_registry/textwrap-0.11.0 \ -> /usr/share/cargo/registry/textwrap-0.11.0/ -
ґଘࠈͱҋͱ (4/4) ▶ Α͘Α͘ௐͯΈΔͱɺclap ͷ Cargo.toml ͕ɺcrates.io ొ࣌ʹґଘ͕ؔม ߋ͞Ε͍ͯΔ༷ʜʜ %
grep -A 1 dependencies.textwrap debian/cargo_registry/clap-2.32.0/Cargo.toml [dependencies.textwrap] version = ">= 0.10, < 0.12" ▶ GitHub ʹ͋Δ clap ͷίʔυͩͱҎԼ [dependencies] bitflags = "1.0" unicode-width = "0.1.4" textwrap = "0.10.0" ▶ Ͳ͏͍͏ج४Ͱมߋ͞Εͨͷ͔·ͩௐ͓ͯΓ·ͤΜʜʜ • debian ͷ librust-*ͳύοέʔδͨͪͲͷΑ͏ͳج४Ͱߋ৽͞Ε͍ͯΔͷͩΖ͏͔ʜʜ -
ͱʹ͔͘ʹ (1/2) ▶ deb Ͱ͖·ͨ͠!! • ͍ɺlintian ͷܯࠂΤϥʔऔͬͯͳ͍Ͱ͕͢!! % dpkg
-I rust-clap-test_0.1.0-1_amd64.deb new Debian package, version 2.0. size 258160 bytes: control archive=648 bytes. 360 όΠτɺ 11 ߦ control 285 όΠτɺ 4 ߦ md5sums Package: rust-clap-test Version: 0.1.0-1 Architecture: amd64 Maintainer: Katsuki Kobayashi <[email protected]> Installed-Size: 770 Depends: libc6 (>= 2.18), libgcc1 (>= 1:4.2) Section: unknown Priority: optional Homepage: <insert the upstream URL, if relevant> Description: <insert up to 60 chars description> <insert long description, indented with spaces> -
ͱʹ͔͘ʹ (2/2) % dpkg -c rust-clap-test_0.1.0-1_amd64.deb drwxr-xr-x root/root 0 2019-03-23
18:06 ./ drwxr-xr-x root/root 0 2019-03-23 18:06 ./usr/ drwxr-xr-x root/root 0 2019-03-23 18:06 ./usr/bin/ -rwxr-xr-x root/root 776600 2019-03-23 18:06 ./usr/bin/clap-test drwxr-xr-x root/root 0 2019-03-23 18:06 ./usr/share/ drwxr-xr-x root/root 0 2019-03-23 18:06 ./usr/share/doc/ drwxr-xr-x root/root 0 2019-03-23 18:06 ./usr/share/doc/rust-clap-test/ -rw-r--r-- root/root 197 2019-03-23 18:06 ./usr/share/doc/rust-clap-test/READ -rw-r--r-- root/root 190 2019-03-23 18:06 ./usr/share/doc/rust-clap-test/chan -rw-r--r-- root/root 1693 2019-03-23 18:06 ./usr/share/doc/rust-clap-test/copy ▶ ͋Ε͚ͩͷϓϩάϥϜͰ 800KB ͔͔ͪ͊͘ʜʜ -
ͱ͏ͱͭʹ·ͱΊ!! ▶ ݁ہ Rust ͳ deb Λ࡞Δʹ • crates.io ʹొͯ͠
debcargo Λϕʔεʹͭ͘Δ͕ެࣜख๏ • ҰԠɺϩʔΧϧͰ dh-cargo Λ͔ͭͬͯରԠͰ͖ͦ͏ • debcargo Λ git ϦϙδτϦʹରԠͤͯ͞ྑͦ͞͏͚ͩͲ Cargo.toml/Cargo.lock ·Θ Γͷ͋Γ? ▶ ͦͦґଘؔେৎ? • ࠓճௐ͖Εͳ͔ͬͨͰ͕͢ɺdeb ͷ librust-*ύοέʔδ·ΘΓ݁ߏ͋Ϳͳ͍ڮΛ ͍ͬͯΔ͔ʜʜ? • ͜ͷ͍͋ͩ (2 ݄) ʹ ripgrep ͕ϏϧυͰ͖ͳ͍ͱ͍͏ buster ͷόά͕͋ͬͨ ͠ʜʜ#920958 ▶ ͦΕͰ Rust ྑ͍ݴޠ • গʑֶशۂઢ͕ΞϨ͚ͩͲɺ͜Ε͔Βྲྀߦ͍ͬͯ͘ʹҧ͍ͳ͍ • Debian ͰίϛϡχςΟ͕׆ൃʹͳΕΑ͍ͳ͊ (ଞਓࣄ) • ॗʑͱ debcargo ։ൃ͞Ε͍ͯΔΑ͏Ͱɺઌि buster ʹೖΒͳ͍͚Ͳ post-install Ͱ test ͕͖ΔΑ͏ʹͳͬͨΓ͍ͯ͠Δ༷ -
Ͳ͔ͬʹૠೖ༧ఆͷϝϞ ▶ Rust ͷϑΝΠϧͷ֦ுࢠ .rs • Rust Ͱॻ͔ΕͨϓϩδΣΫτͷެࣜαΠτ͕ http://xxx.rs Ͱ͋Δ͜ͱ͕ଟ͍
• ࠃผίʔυτοϓυϝΠϯ .rs : ηϧϏΞ ▶ rust-fmt ͱ͍͏πʔϧ͕͋Δ • gofmt ͱ͔ autopep8 ͱ͔ clang-format తͳπʔϧ ▶ Podcast (Rebuild.fm?) Ͱ୭͔͕ݴͬͯͨ • Go: C++͕ݏ͍Ͱۦஞ͍ͨ͠ • Rust: C++͕େ͖͚ͩͲਏ͍ͷͰͳΜͱ͔͍ͨ͠ ▶ Rustacean • LISPer, Pythonista, Rubyist, Gopher, TeXnician తͳ • “crustaceans” : ߕ֪ྨ − ΦϥΠϦʔຊͷදࢴΦΦώϩόΦΪΨχ − ඇެࣜͳϚείοτΧχ (Ferris ͍ͬͯ͏༷) -
Ͳ͔ͬʹૠೖ༧ఆͷϝϞ ▶ ͱΓ͋͑ͣύοέʔδʹ͢Δʹ • dh_make Λ࣮ߦ • debian/rules ͷ dh
ͷҾʹ--buildsystem cargo ΛՃ cp: ’./debian/cargo-checksum.json’ Λ stat Ͱ͖·ͤΜ: \ ͦͷΑ͏ͳϑΝΠϧσΟϨΫτϦ͋Γ·ͤΜ ▶ Ή͎? • cargo-vendor Ͱ֤ crate ͷ checksum ࡞ΕΔ͚Ͳɺmain.rs ͳΞϓϦͲ͏ͨ͠Β? -
Ͳ͔ͬʹૠೖ༧ఆͷϝϞ debian cargo wrapper: running subprocess ([’env’, ’RUST_BACKTRACE=1’, ’/usr/bin/car error:
no matching package named ‘hyper‘ found