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
427 words
2 minutes
240604_Generics_basic01
link
Generic Data Types
- We use generics to create definitions for items like function signatures or structs, which we can then use with many different concrete data types. Let’s first look at how to define functions, structs, enums, and methods using generics. Then, we’ll discuss how generics affect code performance.
- https://doc.rust-lang.org/book/ch10-01-syntax.html
- 일반 데이터 유형
- 우리는 제네릭을 사용하여 함수 서명이나 구조와 같은 항목에 대한 정의를 만들고, 이를 다양한 구체적인 데이터 유형과 함께 사용할 수 있습니다. 먼저 제네릭을 사용하여 함수, 구조, 에넘, 메서드를 정의하는 방법을 살펴보겠습니다. 그런 다음 제네릭이 코드 성능에 미치는 영향에 대해 논의하겠습니다.
Generic을 써야하는 이유
- 내가 젤 싫어하는게 비슷한 코드 반복 작업을 해야한다는 거~ 귀찮다 많이~~
- 이 미친짓을 할수 없다 해서 나온게 Generic
fn add(x: i32, y: i32) -> i32 {
x + y
}
fn add_f64(x: f64, y: f64) -> f64 {
x + y
}
fn main() {
add(2, 2);
add_f64(4.98, 6.90);
}한번에 해결~~ Generic만세
- type마다 똑같은걸 만들수 없다. 뭐 매크로를 만들어서 해결할수 있다.
- 일단 Generic으로 해결해 보자.
// fn add<T: std::ops::Add<Output = T>>(x: T, y: T) -> T
fn add<T>(x: T, y: T) -> T
where
T: std::ops::Add<Output = T>,
{
x + y
}
fn main() {
add(2, 2);
add(2.666, 3.001);
}같이 보면 좋은글
Generics기초
240604_Generics_basic01
https://younghakim7.github.io/blog/posts/240604_generics_basic01/