코딩Coding/★Rust★기초★Syntax

rust에서 타입type_of을 알아보는 방법ex:String의 타입 알아보기

내인생PLUS 2022. 4. 20. 06:36
728x90

 

 

 

 

~/Documents/Project/Github/rust_project/training_rustacean_rust/src/main.rs.html
 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

 

How check type of variable

let x = 21; let y = 2.5; type_of(x) //i32

users.rust-lang.org

 

 

코딩 예문(easy_rust)

https://dhghomon.github.io/easy_rust/Chapter_14.html

 

Strings - Easy Rust

See this chapter on YouTube Rust has two main types of strings: String and &str. What is the difference? &str is a simple string. When you write let my_variable = "Hello, world!", you create a &str. A &str is very fast. String is a more complicated string.

dhghomon.github.io

 

반응형