åã®èšäº
- ã0ã æºå â åå
- ...
- ã11ã Sized ãã¬ã€ãã»From ãã¬ã€ãã»é¢é£å ïœãããããã¬ã€ããšé¢é£åïœ â åå
- ã12ã Cloneã»Copyã»Dropãã¬ã€ã ïœ èŠããã¹ãäž»èŠãã¬ã€ããã¡ ïœ â ä»å
å šèšäºäžèŠ§
- ã0ã æºå
- ã1ã æ§æã»æŽæ°ã»å€æ°
- ã2ã ifã»ãããã¯ã»æŒç¿
- ã3ã å¯å€ã»ã«ãŒãã»ãªãŒããŒãããŒ
- ã4ã ãã£ã¹ãã»æ§é äœ (ããŸã«UFCS)
- ã5ã ããªããŒã·ã§ã³ã»ã¢ãžã¥ãŒã«ã®å ¬éç¯å² ïœ â ã«ãã»ã«åïŒïœ
- ã6ã ã«ãã»ã«åã®ç¶ããšæææš©ãšã»ãã¿ãŒ ïœãããŠäžå€åç §ãšå¯å€åç §ïŒïœ
- ã7ã ã¹ã¿ãã¯ã»ããŒããšåç §ã®ãµã€ãº ïœã¡ã¢ãªã®è©±ïœ
- ã8ã ãã¹ãã©ã¯ã¿(å€æ°ã®çµãã)ã»ãã¬ã€ã ïœçµãããšå§ãŸãïœ
- ã9ã Orphan rule (å€å ã«ãŒã«)ã»æŒç®åãªãŒããŒããŒãã»derive ïœEmpowerment ðª ïœ
- ã10ã ãã¬ã€ãå¢çã»æååã»Derefãã¬ã€ã ïœãã¬ã€ãã®ã¢ã¬ã³ã¬ïœ
- ã11ã Sized ãã¬ã€ãã»From ãã¬ã€ãã»é¢é£å ïœãããããã¬ã€ããšé¢é£åïœ
- ã12ã Cloneã»Copyã»Dropãã¬ã€ã ïœèŠããã¹ãäž»èŠãã¬ã€ããã¡ïœ
- ã13ã ãã¬ã€ããŸãšãã»åæåã»matchåŒ ïœæ匷ã®ãã¬ã€ãã®æ¬¡ã¯ãæ匷ã®åæåïœ
- ã14ã ãã£ãŒã«ãä»ãåæåãšOptionå ïœãã§ã¯ã¯ã¬ã¿ïœ
- ã15ã Resultå ïœRustæµãšã©ãŒãã³ããªã³ã°è¡ïœ
- ã16ã Errorãã¬ã€ããšå€éšã¯ã¬ãŒã ïœäŸåã¯Cargo.tomlã«å šéšãä»»ãïŒïœ
- ã17ã thiserrorã»TryFrom ïœãã¬ã€ããResultãèªç±èªåšïŒïœ
- ã18ã Errorã®ãã¹ãã»æ £äŸçãªæžãæ¹ ïœRustãããã®ç®éãïœ
- ã19ã é åã»åçé å ïœã¹ã¿ãã¯ã䜿ãããé åãšãããŒãã«ä¿åã§ããåçé åïœ
- ã20ã åçé åã®ãªãµã€ãºã»ã€ãã¬ãŒã¿ ïœãŸããŸããã¬ã€ãç»å ŽïŒïœ
- ã21ã ã€ãã¬ãŒã¿ã»ã©ã€ãã¿ã€ã ïœã©ã€ãã¿ã€ã 泚éããããç»å ŽïŒïœ
- ã22ã ã³ã³ãããŒã¿ã»RPIT ïœ ã
Iterator
ãã¬ã€ããå®è£ ããŠããã€ãïœ - ã23ã
impl Trait
ã»ã¹ã©ã€ã¹ ïœé åã®æ¬ çïœ - ã24ã å¯å€ã¹ã©ã€ã¹ã»äžæžãæ§é äœ ïœæ§é äœã§ç¶æ è¡šçŸïœ
- ã25ã ã€ã³ããã¯ã¹ã»å¯å€ã€ã³ããã¯ã¹ ïœã€ã³ããã¯ã¹ããã¬ã€ãïŒïœ
- ã26ã HashMapã»é åºã»BTreeMap ïœRustã®èŸæžåïœ
- ã27ã ã¹ã¬ããã»'staticã©ã€ãã¿ã€ã ïœäžŠååŠçã«èŠãRustã®æ©æµïœ
- ã28ã ãªãŒã¯ã»ã¹ã³ãŒãä»ãã¹ã¬ãã ïœã©ã€ãã¿ã€ã ã«æããïŒïœ
- ã29ã ãã£ãã«ã»åç
§ã®å
éšå¯å€æ§ ïœRustã®èã®åãmpscãš
Rc<RefCell<T>>
ïœ - ã30ã åæ¹åéä¿¡ã»ãªãã¡ã¯ã¿ãªã³ã° ïœè¿ä¿¡çšå°çãå ¥ãããïŒïœ
- ã31ã äžéä»ããã£ãã«ã»PATCHæ©èœ ïœãã³ã¯ããªãããã«å¶åŸ¡ïŒïœ
- ã32ã
Send
ã»æä»çããã¯(Mutex
)ã»é察称æä»çããã¯(RwLock
) ïœçæArc<Mutex<T>>
ç»å Žïœ - ã33ã ãã£ãã«ãªãã§å®è£ ã»Syncã®è©± ïœèå¯åã§ãïœ
- ã34ã
async fn
ã»éåæã¿ã¹ã¯çæ ïœRustã®éåæå ¥éïœ - ã35ã éåæã©ã³ã¿ã€ã ã»Futureãã¬ã€ã ïœéåæã®ãäœæ³ïœ
- ã36ã ããããã³ã°ã»éåæçšã®å®è£ ã»ãã£ã³ã»ã« ïœã©ã¹ãã¹ããŒãïŒïœ
- ã37ã Axumã§ã¯ã©ãµãïŒ ïœæçµåïœ
- ãããŸã1ã Rustã§åéãããŠããããš3éž ððŽ ã100 Exercises To Learn Rust ðŠ å®èµ°èšäº ðã
- ãããŸã2ã ãð æºæ ðãQiitanã°ããºæ¬²ããã«1æ¥ã«33èšäºæçš¿ãã話ãŸãã¯Qiita CLIãšcargo scriptãåžæããçãªäœã
100 Exercise To Learn Rust æŒç¿ç¬¬12åã«ãªããŸãïŒä»å玹ä»ãããã¬ã€ãéã¯Rustã§ããã°ã©ãã³ã°ããéã«åžžã«éèŠã«ãªã£ãŠããè ãã¡ã§ãã
ä»åã®é¢é£ããŒãž
[04_traits/11_clone] Cloneãã¬ã€ã
åé¡ã¯ãã¡ãã§ãã
// TODO: add the necessary `Clone` implementations (and invocations)
// to get the code to compile.
pub fn summary(ticket: Ticket) -> (Ticket, Summary) {
(ticket, ticket.summary())
}
pub struct Ticket {
pub title: String,
pub description: String,
pub status: String,
}
impl Ticket {
pub fn summary(self) -> Summary {
Summary {
title: self.title,
status: self.status,
}
}
}
pub struct Summary {
pub title: String,
pub status: String,
}
Clone
ãã¬ã€ããå®è£
ããŠåŒãã§ã»ããããšããåé¡ã§ãã
解説
Clone
ãã¬ã€ããå®è£
ããŠåŒã¶ã ãã§ããã
// TODO: add the necessary `Clone` implementations (and invocations)
// to get the code to compile.
pub fn summary(ticket: Ticket) -> (Ticket, Summary) {
- (ticket, ticket.summary())
+ (ticket.clone(), ticket.summary())
}
+ #[derive(Clone)]
pub struct Ticket {
pub title: String,
pub description: String,
pub status: String,
}
impl Ticket {
pub fn summary(self) -> Summary {
Summary {
title: self.title,
status: self.status,
}
}
}
pub struct Summary {
pub title: String,
pub status: String,
}
Clone
ãã¬ã€ãã¯è€è£œã®ãã¬ã€ãã§ããæææš©ã«å³ããRustã§ãå€æŽãå
±æããã€ããããªãæ§é äœã®è€è£œã¯ã Clone
ãã¬ã€ãã® .clone()
ã¡ãœãããåŒã¶ããšã«ãã£ãŠè¡ãããŸãã
ããªãéèŠãªãã¬ã€ãã§ã¯ãããŸãããæ§é äœã«é¢ããŠã¯å¹žãã«ããŠå
šãã£ãŒã«ãã« Clone
ãã¬ã€ããå®è£
ãããŠããã°æåå®è£
ããšãŠã楜ãªãã¬ã€ãã§ã(ããã§ãªãå Žåã§ãæåå®è£
å¯èœã§ã)ãæžãåºããŠã¿ããš .clone()
ã¡ãœãããäœãè¡ãã®ãã¯äžç®çç¶ã§ãããã
struct Hoge {
a: usize,
b: String,
c: Vec<bool>,
}
impl Clone for Hoge {
// å
ã®å€æ°ã®æææš©ã¯å¥ªãããæ°ããæ§é äœã€ã³ã¹ã¿ã³ã¹ãè¿ããŠãã
fn clone(&self) -> Self {
Self {
// è€è£œãããŠãã
a: self.a.clone(),
b: self.b.clone(),
c: self.c.clone(),
}
}
}
ãããŠãã®åŠçãèªåå®è£
ããŠãããã®ã Clone
deriveãã¯ãã§ãã Clone
ãã¯ãã䜿ãã«ã¯ãå
šãã£ãŒã«ãã®åã Clone
ãã¬ã€ããå®è£
ããŠããå¿
èŠããããŸãã äžæã«æåå®è£
ããããããã®å¶çŽãå®ããªããderiveãã¯ãã§å®è£
ããã®ãå®ç³ã§ãããã
ãã®ãã¯ãã䜿çšã㊠Clone
ãå®è£
ãããšå¬ããããšãšããŠã .clone()
ã¡ãœãããåŒã³åºããšè€è£œãååž°çã«åŒã³åºãããŸããããªãã¡ããã©ã«ã㧠ãã£ãŒãã³ããŒããã ããã§ãïŒ
æææš©ãåç §ãšããä»çµã¿ããããŠãããŸã§ã®æŒç¿ããããããããå¯ããã€ããããããŸãããã Rustã«ã¯ã·ã£ããŒã³ããŒãšããèãæ¹ã¯åºæ¬çã«ãããŸãã 1ã ãã¡ãã工倫ãããã°ã·ã£ããŒã³ããŒã«æºããå®è£ ãããããšãå¯èœã§ããããã®å Žåã¡ãã£ãšå€§æããã«ãªã(第29åããäžèšã«ç€ºãæèãåç §ããŠã»ããã§ã)ã®ã§æ®æ®µã¯æ°ã«ããå¿ èŠããããŸãããä»èšèªã§æ£ã ããã°ã©ããæãããã®çœ ã¯ãRustã«ã¯åºæ¬éçšããªãã®ã§ãïŒ
ãã£ãšè©³ãã話ã¯æèãèªãã§ãã ãããšææŠè³æ¥µã§ãïŒ
[04_traits/12_copy] Copyãã¬ã€ã
åé¡ã¯ãã¡ãã§ãã
// TODO: implement the necessary traits to make the test compile and pass.
// You *can't* modify the test.
pub struct WrappingU32 {
value: u32,
}
impl WrappingU32 {
pub fn new(value: u32) -> Self {
Self { value }
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_ops() {
let x = WrappingU32::new(42);
let y = WrappingU32::new(31);
let z = WrappingU32::new(u32::MAX);
assert_eq!(x + y + y + z, WrappingU32::new(103));
}
}
ããã¹ããå€ããã«ã³ã³ãã€ã«ãéããŠããåé¡ã«ãªããŸãã
解説
.clone()
ã¡ãœãããåŒã³åºãããŠããªãã®ã§ Clone
ãã¬ã€ãã ãã§ã¯äžååã§ãã ããŒã«ãŒãã¬ã€ã Copy
ãå®è£
ããããšã§ãæææš©ã奪ããããã«ãªã£ããèªåè€è£œãããããã«ããŸãã
use std::ops::Add;
+ #[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub struct WrappingU32 {
value: u32,
}
impl WrappingU32 {
pub fn new(value: u32) -> Self {
Self { value }
}
}
impl Add for WrappingU32 {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self {
value: self.value.wrapping_add(rhs.value),
}
}
}
Copy
ãã¬ã€ããå®è£
ããã«ã¯ãåæãšã㊠Clone
ãã¬ã€ããå®è£
ãããŠããå¿
èŠããããããåçã«ã¯ Clone
ãã¬ã€ããä»ããŠããŸãã
Copy
ãã¬ã€ãã®ä»äžã«ã¯ããäžã€æ¡ä»¶ããã£ãŠãå
å
ãããã£ãŒã«ãå
šãŠã Copy
ãã¬ã€ããå®è£
ããŠãã å¿
èŠããããŸãããããŠã Copy
ãå®è£
ããŠããåã¯å€§äœ ã¹ã¿ãã¯äžã«å€ãä¿æãããããªããã£ãå ã§ããããŒãã¢ãã±ãŒã·ã§ã³ãå¿
èŠãªå( String
ã Vec<T>
ãªã©)ãå
å
ããŠããæ㯠Copy
ã¯å®è£
ã§ããªããšèããŠãããŸãããã
Clone
ãã¬ã€ããä»äžããŠããããšããå
éšçã« .clone()
ãåŒãã§ãããšåéãããã¡ã§ãã(å®éçè
ã¯åéãããŠããŸããã)ã Copy
ãã¬ã€ããä»äžãããåã®è€è£œã¯ãã¹ã¿ãã¯ã¡ã¢ãªäžã®å€ããã®ãŸãŸå¥ãªå Žæã«è€è£œãããã ã㧠.clone()
ã¯åŒã°ããŸããïŒ
Copy
ãå®è£
ããŠããå㧠.clone()
ã«ãŠå€ãã£ãåŠçãå®è£
ããããšã¯å¯èœã§ããããã®å ŽåæãŸãªãçµæã«ãªãããšã容æã«äºæ³ãããã®ã§ããããŠãããæ¹ãè¯ãããã§ãã
Copy
ã«ããèªåè€è£œã§ã¯ .clone()
ãåŒã°ããŠããªãããšã芳枬ã§ããŸãã
[04_traits/13_drop] Dropãã¬ã€ã
åé¡ã¯ãã¡ãã§ãã
// TODO: implement a so-called "Drop bomb": a type that panics when dropped
// unless a certain operation has been performed on it.
// You can see the expected API in the tests below.
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[should_panic]
fn test_drop_bomb() {
let bomb = DropBomb::new();
// The bomb should panic when dropped
}
#[test]
fn test_defused_drop_bomb() {
let mut bomb = DropBomb::new();
bomb.defuse();
// The bomb should not panic when dropped
// since it has been defused
}
}
ãããããåã«ãªãœãŒã¹ããã£ãããšè§£æŸããŠããªããšãããã¯ã«ãªãããDropç匟ããå®è£ ããŠãã ããããšããåé¡ã§ãã
解説
Rustã®ãªãœãŒã¹ç®¡ç䟿å©ãã¬ã€ãã Drop
ã®ç»å Žã§ãïŒãã®ãã¬ã€ãã«ããæäŸããã drop
ã¡ãœããã¯ã å€æ°ã®ããããæã«åŒã³åºãããŸã ïŒ
struct DropBomb {
defused: bool,
}
impl DropBomb {
fn new() -> Self {
Self { defused: false }
}
fn defuse(&mut self) {
self.defused = true;
}
}
impl Drop for DropBomb {
fn drop(&mut self) {
if !self.defused {
panic!("Resources are not yet cleaned up! Please call `defuse` method before drop to clean up it.");
}
}
}
ãã®æ§è³ªãå©çšããŠãã¹ããéããŸãïŒDropBombã®å€ã®ã©ã€ãã¿ã€ã ãçµäºããŠãããããããéã« drop
ã¡ãœããã®äžèº«ãåŒã°ããã®ã§ã defuse
ã¡ãœããã§ãããããç¶ãã¹ããªãœãŒã¹è§£æŸåŠçãããŠããªããšããããã¯ããŠããŸãããšãã寞æ³ã§ãã
Drop
ãã¬ã€ããå®è£
ããã°ãRustã®ã©ã€ãã¿ã€ã ã®ä»çµã¿ãåããŠããªãœãŒã¹ç®¡çãã§ããã®ã§ãïŒPythonã® with æãC#ã®useãããã¯ã«è¿ãæãã§ãããã...ïŒ
ããã¯äŸãã°ä»¥åå°ã觊ããããã«ãã¡ã€ã«ãã³ãã©ãããããŠDBã®ã³ãã¯ã·ã§ã³ããã©ã³ã¶ã¯ã·ã§ã³çã«ãå¿çšã§ããŸãã
å®éã®å®è£ äŸ: https://github.com/sfackler/rust-postgres/blob/a1bdd0bf15325cdc05fcfe3c0671953b2a8c7981/postgres/src/transaction.rs#L16
ãã©ã³ã¶ã¯ã·ã§ã³çã® close
ã®åŒã³å¿ããæ°ã«ããªããŠè¯ãã®ã§ããšãŠã匷åãªãã¬ã€ãã§ãããåžžã«æèããŠããããã§ãã
Bookã«æžãããŠããããšã§ããã Copy
ãã¬ã€ããš Drop
ãã¬ã€ãã¯å
±åã§ããŸããïŒ ããããçè
ç¥ããªãã£ãã...
Copy
ãã¬ã€ããå®è£
ãããåã¯ã¹ã¿ãã¯äžã§ããæ±ãããªã軜ãåã§ãããšããã³ã³ã»ãããéã¿ããšããªãœãŒã¹çä»ããç®çãšãã Drop
ãã¬ã€ããå
±åäžå¯ãšããã®ã¯çŽåŸã§ãã話ã§ã¯ãããŸãã
ã§ã¯æ¬¡ã®åé¡ã«è¡ããŸãããïŒ
次ã®èšäº: ã13ã ãã¬ã€ããŸãšãã»åæåã»matchåŒ ïœæ匷ã®ãã¬ã€ãã®æ¬¡ã¯ãæ匷ã®åæåïœ
ç»å ŽããPlayground
(å®éã«ç¡å¹åããããšã¯ãªãã§ããã)Rust Playgroundäžã®ããŒã¿ãåªå€±ããå¯èœæ§ãéã¿ãŠãäžå¿èšäºã«ããœãŒã¹ã³ãŒããæ²èŒããããšãšããŸããã
#[derive(Debug)]
struct Hoge(u32);
impl Clone for Hoge {
fn clone(&self) -> Self {
println!("beep!: {}", self.0);
Hoge(self.0)
}
}
impl Copy for Hoge {}
fn main() {
let h = Hoge(10);
let h2 = h;
dbg!(h);
dbg!(h2);
dbg!(h);
h.clone();
}
-
ãåç §ãè€è£œãããæ§ã¯ã·ã£ããŒã³ããŒã§ã¯ïŒããšããããã³ããæ¥ããã§ãããããªãã§ãããããããªé£ããèšãæ¹ãããã®ã§ããïŒãåç §ãè€è£œãããããšè¡šçŸããã°è¯ããªãã§ããïŒããšè¿ããããªã£ã¡ãããã§ããããããšããã·ã£ããŒã³ããŒãšããèãæ¹ããªãããšããããã¯ãããããèšãåããããªãããšããæ¹ãæ£ç¢ºãªã®ãããããŸããã â©