From 02d29bc588604af9f5b21a10d55bbefcd7a0f6d7 Mon Sep 17 00:00:00 2001 From: Eiji Tanaka Date: Wed, 18 May 2022 14:29:24 +0900 Subject: [PATCH] 20220518 newtype, associated type --- 14/14-7_newtype_1.rs | 30 ++++++++++++++++++ 14/14-7_newtype_2.rs | 7 +++++ 14/14-8-2_associated_types.rs | 58 +++++++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+) create mode 100644 14/14-7_newtype_1.rs create mode 100644 14/14-7_newtype_2.rs create mode 100644 14/14-8-2_associated_types.rs diff --git a/14/14-7_newtype_1.rs b/14/14-7_newtype_1.rs new file mode 100644 index 0000000..05b3cea --- /dev/null +++ b/14/14-7_newtype_1.rs @@ -0,0 +1,30 @@ +struct Years(i64); + +struct Days(i64); + +impl Years { + pub fn to_days(&self) -> Days { + Days(self.0 * 365) + } +} + + +impl Days { + /// truncates partial years + pub fn to_years(&self) -> Years { + Years(self.0 / 365) + } +} + +fn old_enough(age: &Years) -> bool { + age.0 >= 18 +} + +fn main() { + // let age = Years(5); + let age = Years(18); + let age_days = age.to_days(); + println!("Old enough {}", old_enough(&age)); + println!("Old enough {}", old_enough(&age_days.to_years())); + // println!("Old enough {}", old_enough(&age_days)); +} diff --git a/14/14-7_newtype_2.rs b/14/14-7_newtype_2.rs new file mode 100644 index 0000000..efefeb4 --- /dev/null +++ b/14/14-7_newtype_2.rs @@ -0,0 +1,7 @@ +struct Years(i64); + +fn main() { + let years = Years(42); + let years_as_primitive_1: i64 = years.0; // Tuple + let Years(years_as_primitive_2) = years; // Destructuring +} \ No newline at end of file diff --git a/14/14-8-2_associated_types.rs b/14/14-8-2_associated_types.rs new file mode 100644 index 0000000..132c954 --- /dev/null +++ b/14/14-8-2_associated_types.rs @@ -0,0 +1,58 @@ +struct Container(i32, i32); + +// A trait which checks if 2 items are stored inside of container. +// Also retrieves first or last value. +// 2つの要素がコンテナ型の中に保持されていることを確認するトレイト。 +// また、最初あるいは最後の要素を取り出すこともできる。 +trait Contains { + // Define generic types here which methods will be able to utilize. + // メソッドが使用できるジェネリック型を定義 + type A; + type B; + + fn contains(&self, _: &Self::A, _: &Self::B) -> bool; + fn first(&self) -> i32; + fn last(&self) -> i32; +} + +impl Contains for Container { + // Specify what types `A` and `B` are. If the `input` type + // is `Container(i32, i32)`, the `output` types are determined + // as `i32` and `i32`. + // `A`と`B`がどの型であるかを明示。インプットの型(訳注: つまり`Self`の型) + // が`Container(i32, i32)`である場合、出力型は`i32`と`i32`となる。 + type A = i32; + type B = i32; + + // `&Self::A` and `&Self::B` are also valid here. + // `&i32`の代わりに`&Self::A`または`&self::B`と書いても良い + fn contains(&self, number_1: &i32, number_2: &i32) -> bool { + (&self.0 == number_1) && (&self.1 == number_2) + } + // Grab the first number. + // 1つ目の数を取得 + fn first(&self) -> i32 { self.0 } + + // Grab the last number. + // 最後の数を取得 + fn last(&self) -> i32 { self.1 } +} + +fn difference(container: &C) -> i32 { + container.last() - container.first() +} + +fn main() { + let number_1 = 3; + let number_2 = 10; + + let container = Container(number_1, number_2); + + println!("Does container contain {} and {}: {}", + &number_1, &number_2, + container.contains(&number_1, &number_2)); + println!("First number: {}", container.first()); + println!("Last number: {}", container.last()); + + println!("The difference is: {}", difference(&container)); +}