Skip to content

Commit

Permalink
19_concepts_Avoid_reflection
Browse files Browse the repository at this point in the history
  • Loading branch information
sotanengel committed Jan 13, 2025
1 parent bbbfd02 commit 7698ac3
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ bin = [
{ name = "1_required_and_default_methods_sol", path = "solutions/13_traits_Use_default_implementations_to_minimize_required_trait_methods/1_required_and_default_methods.rs" },
{ name = "1_hint_for_panic", path = "exercises/18_concepts_Dont_panic/1_hint_for_panic.rs" },
{ name = "1_hint_for_panic_sol", path = "solutions/18_concepts_Dont_panic/1_hint_for_panic.rs" },
{ name = "1_check_unique_id", path = "exercises/19_concepts_Avoid_reflection/1_check_unique_id.rs" },
{ name = "1_check_unique_id_sol", path = "solutions/19_concepts_Avoid_reflection/1_check_unique_id.rs" },
]

[package]
Expand Down
17 changes: 17 additions & 0 deletions exercises/19_concepts_Avoid_reflection/1_check_unique_id.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use std::any::Any;

// TODO: TにAnyトレイト境界を設けて、グローバルにユニークな識別子(数値)を出力するようにしてください。
fn print_type_id<T>(value: &T) {
let type_id;
let type_id = println!("The TypeId of the value is: {:?}", type_id);
}

fn main() {
let value1 = 42;
let value2 = "Hello, Rust!";
let value3 = 3.14;

print_type_id(&value1);
print_type_id(&value2);
print_type_id(&value3);
}
7 changes: 7 additions & 0 deletions info.toml
Original file line number Diff line number Diff line change
Expand Up @@ -256,3 +256,10 @@ dir = "18_concepts_Dont_panic"
test = true
hint = """Rustではパニック処理を適切に扱う方法を学びます。適切なエラーハンドリングを実装して、安全なコードを書く方法を確認しましょう。"""
skip_check_unsolved = false

[[exercises]]
name = "1_check_unique_id"
dir = "19_concepts_Avoid_reflection"
test = false
hint = """リフレクションを避けつつ、一意のIDを確認する方法を学びます。"""
skip_check_unsolved = false
16 changes: 16 additions & 0 deletions solutions/19_concepts_Avoid_reflection/1_check_unique_id.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use std::any::Any;

fn print_type_id<T: Any>(value: &T) {
let type_id = value.type_id();
println!("The TypeId of the value is: {:?}", type_id);
}

fn main() {
let value1 = 42;
let value2 = "Hello, Rust!";
let value3 = 2.468;

print_type_id(&value1);
print_type_id(&value2);
print_type_id(&value3);
}

0 comments on commit 7698ac3

Please sign in to comment.