H-advanced/Generics.rs hinzugefügt

main
Vincent Laux 2026-06-16 02:07:02 +02:00
parent 9e987d3c9b
commit 697827586f
1 changed files with 15 additions and 0 deletions

View File

@ -0,0 +1,15 @@
// Ohne Generics viel Duplizierung
fn max_i32(a: i32, b: i32) -> i32 { ... }
fn max_f64(a: f64, b: f64) -> f64 { ... }
// Mit Generics ein mal schreiben
fn max<T: PartialOrd>(a: T, b: T) -> T {
if a > b { a } else { b }
}
// Verwendung
println!("{}", max(3, 7)); // 7
println!("{}", max(3.14, 2.7)); // 3.14