ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Rust연습]Cow part2
    코딩Coding/Rust연습 2022. 4. 19. 23:01
    728x90

     

     

     

    ~/Documents/Project/Github/rust_project/training_rustacean_rust/src/main.rs.html
     1 use std::borrow::Cow;
     2 
     3 #[derive(Debug)]
     4 struct User<'a> {
     5     name: Cow<'a, str>,
     6 }
     7 
     8 impl User<'_> {
     9     fn is_borrowed(&self) {
    10         match &self.name {
    11             Cow::Borrowed(name) => println!("It's Borrowed: {name}"),
    12             Cow::Owned(name) => println!("It's owned: {name}"),
    13         }
    14     }
    15 }
    16 
    17 fn main() {
    18     let user_1 = User {
    19         name: "user_1".into(),
    20     };
    21 
    22     let user_2 = User {
    23         name: "User 2".to_string().into(),
    24     };
    25 
    26     user_1.is_borrowed();
    27     user_2.is_borrowed();
    28 }
    

     

     

     

     

     

     

     

    결과

       Compiling training_rustacean_rust v0.1.0 (/Users/globalyoung/Documents/Project/Github/rust_project/training_rustacean_rust)
        Finished dev [unoptimized + debuginfo] target(s) in 0.11s
         Running `target/debug/training_rustacean_rust`
    It's Borrowed: user_1
    It's owned: User 2
    반응형
Designed by Tistory.