Cmp Functions
Comparison functions และ utilities
min และ max
Section titled “min และ max”use std::cmp::{min, max};
fn main() { // Basic usage println!("=== min / max ==="); println!("min(5, 10): {}", min(5, 10)); println!("max(5, 10): {}", max(5, 10));
// With expressions let a = 42; let b = 17; let c = 28;
println!("\nMultiple values:"); println!("max of three: {}", max(max(a, b), c)); println!("min of three: {}", min(min(a, b), c));
// Chained let values = [5, 2, 8, 1, 9, 3]; let max_val = values.iter().copied().max().unwrap(); let min_val = values.iter().copied().min().unwrap();
println!("\nFrom iterator:"); println!("max: {}", max_val); println!("min: {}", min_val);
// With Option let maybe_a: Option<i32> = Some(5); let maybe_b: Option<i32> = Some(10);
println!("\n=== Option Comparison ==="); println!("min(Some(5), Some(10)): {:?}", min(maybe_a, maybe_b)); println!("max(Some(5), None): {:?}", max(Some(5), None)); // None < Some for any value}เข้าสู่ระบบเพื่อดูเนื้อหาเต็ม
ยืนยันตัวตนด้วยบัญชี Google เพื่อปลดล็อกบทความทั้งหมด
Login with Google