코딩Coding/★Rust★기초★Syntax
rust에서 타입type_of을 알아보는 방법ex:String의 타입 알아보기
내인생PLUS
2022. 4. 20. 06:36
728x90
1 use std::any::type_name; 2 3 fn type_of<T>(_: T) -> &'static str { 4 type_name::<T>() 5 } 6 7 fn main() { 8 let my_name = "Billybrobby"; 9 let my_country = "USA"; 10 let my_home = "Korea"; 11 12 println!("{}", type_of(my_name)); 13 let together = format!( 14 "I am {} and I come from {} but I live in {}.", 15 my_name, my_country, my_home 16 ); 17 let my_string: String = "Try to make this a String".into(); 18 println!("{my_string}"); 19 } 20
결과
cargo run
Compiling training_rustacean_rust v0.1.0 (/Users/globalyoung/Documents/Project/Github/rust_project/training_rustacean_rust)
warning: unused variable: `together`
--> src/main.rs:13:9
|
13 | let together = format!(
| ^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_together`
|
= note: `#[warn(unused_variables)]` on by default
warning: `training_rustacean_rust` (bin "training_rustacean_rust") generated 1 warning
Finished dev [unoptimized + debuginfo] target(s) in 0.08s
Running `target/debug/training_rustacean_rust`
&str
Try to make this a String
출처 :
https://users.rust-lang.org/t/how-check-type-of-variable/33845
코딩 예문(easy_rust)
https://dhghomon.github.io/easy_rust/Chapter_14.html
반응형