-
Rust연습]Cow part2코딩Coding/Rust연습 2022. 4. 19. 23:01728x90
~/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
반응형'코딩Coding > Rust연습' 카테고리의 다른 글
Rust연습❤️) Collect & Vec 조합 Closure활용 (0) 2022.06.23 Rust연습)그래프 그리기 + 움직이는 별 표현하기(움직이는 gif만들기) (0) 2022.06.15 Rust연습] tuple연습❤️ & ndarray ❤️연습 (0) 2022.06.02 Rust연습] Cow__120 Easy Rust in Korean: to_mut with Cow (0) 2022.04.19 Rust연습] cow_빨라서 최고borrow에서 적극 활용하자 (0) 2022.04.19 rust연습]vec벡터 끼리 합치기 + vec + vec append (0) 2022.04.18 rust연습]u8_sting_stack저장하기bytes() / std::str (0) 2022.04.18 rust연습]10진수 숫자 16진수로 알아보는 방법& 16진수를 10진수 숫자로 알아보는 방법 (0) 2022.04.17