Panic Functions
Functions และ macros สำหรับ panic handling และ program termination ใน Rust ใช้สำหรับ unrecoverable errors และ bugs ในโปรแกรม
1. panic! Macro
Section titled “1. panic! Macro”panic! หยุดการทำงานของ thread ทันทีและเริ่ม unwinding stack เป็นการบอกว่าเกิดสถานการณ์ที่โปรแกรมไม่สามารถ handle ได้
การใช้ panic!
Section titled “การใช้ panic!”fn main() { // Basic panic with message // panic!("Something went wrong!");
// Panic with formatting let x = 42; // panic!("Error at position {}", x);
// Common causes of panic let v = vec![1, 2, 3]; // v[10]; // Index out of bounds
// Unwrap on None let opt: Option<i32> = None; // opt.unwrap(); // Panics
// Unwrap on Err let result: Result<i32, &str> = Err("error"); // result.unwrap(); // Panics
println!("No panic occurred");}เข้าสู่ระบบเพื่อดูเนื้อหาเต็ม
ยืนยันตัวตนด้วยบัญชี Google เพื่อปลดล็อกบทความทั้งหมด
Login with Google