Categories
Tags
3d algorithms alignment analyze APIT Arc Architecture arm ascii assembly asynchronous base64 BitHacks Blogging box c c23 clang clang-format client clippy cmake compiler Computer concat concurrency const_fn constexpr contravariant cos covariant cpp cpu crate CS Customization cybersecurity DataStructure db debugging Demo deserialization discrete doc DP drawio dtruss Dynamic emulator example Example FFI flamegraph flat_map fold format FP fsanitize Functional FunctionalProgramming functions futures Fuwari game GATs gcc gccrs generics gitignore glibc GUI hacking hashmap haskell heap hyperfine Imperative interop invariant iterator join justfile kernel LaTeX leak LFU linux lto MachineLearning macOS map Markdown math ML mmap mod nc OnceLock optimization OS ownership panic parallels perf physics pin postgresql product profiling pub radare2 rayon release reverse RPIT rust sanitizer Science science serialization server shift sin size SmallProjects socket std strace String StringView strip strlen struct sum super surrealdb SWAR swisstable synchronous tan thread time toml tracing traits triangulation uint32_t UnsafeRust utf16 utf8 Video vulkan wsl x86_64 xilem zig
248 words
1 minutes
260107_traits04_clone_copy
Clone 구현하기
derive macro를 써서 손 쉽게 구현
error code
- OwnerShip(오너쉽) 위반으로 실행 되지 않는다.
struct Young {
data: String,
}
fn main() {
let my_data = Young {
data: "test".to_string(),
};
let my_data_clone = my_data;
println!("my_data : {:?}", my_data);
}cargo r을 하게 되면 러스트가 Clone을 구현하라고 에러메세지를 알려준다.
consider implementing `Clone` for this type
...
복잡한 타입일수록 내가 구현해줘야한다.
derive macro를 써서 날로 먹어도 되고
#[derive(Debug, Clone)]
struct Young {
data: String,
}
fn main() {
let my_data = Young {
data: "test".to_string(),
};
let my_data_clone = my_data.clone();
println!("my_data : {:?}", my_data);
}Clone을 구현해 보자- 코드가 복잡할 수록 내가 구현 할줄 알아야한다.
- Rust 는 Zero Cost Abstraction이라는 것만 기억하면된다.
- 등가교환이다. 꽁짜는 없다.
#[derive(Debug)]
struct Young {
data: String,
}
impl Clone for Young {
fn clone(&self) -> Self {
Self {
data: self.data.clone(),
}
}
}
fn main() {
let my_data = Young {
data: "test".to_string(),
};
let my_data_clone = my_data.clone();
println!("my_data : {:?}", my_data);
}260107_traits04_clone_copy
https://younghakim7.github.io/blog/posts/260107_traits04_clone_copy/