-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bbbfd02
commit 7698ac3
Showing
4 changed files
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
exercises/19_concepts_Avoid_reflection/1_check_unique_id.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
solutions/19_concepts_Avoid_reflection/1_check_unique_id.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |