Panics

๋Ÿฌ์ŠคํŠธ๋Š” ๋Ÿฐํƒ€์ž„์—์„œ ์น˜๋ช…์ ์ธ ์˜ค๋ฅ˜๋ฅผ ๋งŒ๋‚˜๋ฉด ํŒจ๋‹‰์„ ๋ฐœ์ƒํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค:

Rust will trigger a panic if a fatal error happens at runtime:

fn main() {
    let v = vec![10, 20, 30];
    println!("v[100]: {}", v[100]);
}
  • ํŒจ๋‹‰์€ ๋ณต๊ตฌํ•  ์ˆ˜ ์—†๊ณ  ์˜ˆ์ƒ์น˜ ๋ชปํ•œ ์˜ค๋ฅ˜์ž…๋‹ˆ๋‹ค.
    • ํŒจ๋‹‰์€ ํ”„๋กœ๊ทธ๋žจ ๋ฒ„๊ทธ์˜ ์ฆ์ƒ์ž…๋‹ˆ๋‹ค.
  • ์ถฉ๋Œ(ํฌ๋ž˜์‹œ)๋ฅผ ํ—ˆ์šฉํ•˜์ง€ ์•Š์•„์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ ํŒจ๋‹‰์„ ์œ ๋ฐœํ•˜์ง€ ์•Š๋Š”(non-panicking) API๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.(Vec::get ๋“ฑ)
  • Panics are for unrecoverable and unexpected errors.
    • Panics are symptoms of bugs in the program.
  • Use non-panicking APIs (such as Vec::get) if crashing is not acceptable.

์—ญ์ฃผ

  • ์œ„ ์˜ˆ์ œ์—์„œ v[100]์„ v.get(100) ์œผ๋กœ ๋Œ€์ฒดํ•ด๋ณด์„ธ์š”