Skip to content
เข้าสู่ระบบ

Time - เวลา

std::time สำหรับวัดเวลา delay และจัดการ duration! เป็น module ที่จำเป็นสำหรับ benchmarking, timeouts, และ scheduling

  • Duration - ช่วงเวลา (5 seconds, 100 ms)
  • Instant - เวลาสำหรับวัด (monotonic clock)
  • SystemTime - เวลาจริง (wall clock)
use std::time::Duration;
fn main() {
// ============================================
// สร้าง Duration จาก units ต่างๆ
// ============================================
let five_secs = Duration::from_secs(5);
let hundred_millis = Duration::from_millis(100);
let fifty_micros = Duration::from_micros(50);
let ten_nanos = Duration::from_nanos(10);
println!("5 seconds: {:?}", five_secs);
println!("100 ms: {:?}", hundred_millis);
println!("50 µs: {:?}", fifty_micros);
println!("10 ns: {:?}", ten_nanos);
// ============================================
// จาก float (seconds)
// ============================================
let half_sec = Duration::from_secs_f64(0.5);
let pi_sec = Duration::from_secs_f64(3.14159);
println!("\n0.5 sec: {:?}", half_sec);
println!("π sec: {:?}", pi_sec);
// ============================================
// Duration::new(secs, nanos)
// ============================================
let d = Duration::new(5, 500_000_000); // 5.5 seconds
println!("\nDuration::new(5, 500_000_000): {:?}", d);
// ============================================
// Constants
// ============================================
println!("\nZERO: {:?}", Duration::ZERO);
println!("MAX: {:?}", Duration::MAX);
println!("SECOND: {:?}", Duration::from_secs(1));
}

เข้าสู่ระบบเพื่อดูเนื้อหาเต็ม

ยืนยันตัวตนด้วยบัญชี Google เพื่อปลดล็อกบทความทั้งหมด