PR3-Rust-SS26/H-advanced/Generics.rs

16 lines
329 B
Rust
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

// 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