panic: attempt to divide by zero Rust Origin: Application & API OSI layer 7 · Application Last updated 5 Nov 2026

Rust: thread 'main' panicked at src/main.rs:3:13: attempt to divide by zero

Integer division or remainder by zero.

Exact message you may see thread 'main' panicked at src/main.rs:3:13: attempt to divide by zeroattempt to calculate the remainder with a divisor of zerothread 'main' panicked: attempt to divide by zeroattempt to divide by zero Rust panic
Seen on: Rust

Meaning

Check the divisor (if d != 0), use checked_div/checked_rem, or return an error. Floating-point division yields infinity/NaN instead of panicking. Typical sources: average of an empty list, user input, counters.

Common causes

  • Divisor is 0 (empty data count)
  • Modulo by zero
  • User-supplied divisor
  • Uninitialised counter
  • Casting float to int 0

⚡ Quick fix

  1. a.checked_div(b).ok_or(Error::DivZero)?
  2. Guard: if b == 0 { return None; }
  3. Use floats if infinity is acceptable

Detailed fix by platform

Rust

  1. let avg = sum.checked_div(count).unwrap_or(0);

How to diagnose

  1. Divisor — Zero when?
  2. Source — Empty data?
  3. Alternative — checked_div?

🔧 Still not fixed?

Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:

Where this error sits

Origin: Application & API. Errors that are often confused with or caused by this one (click to open):

panic: already borrowed: BorrowMutError — Rust: thread 'main' panicked at src/main.rs:8:9: already borrowed: BorrowMutError / already mutably borrowed: BorrowError (Rust)panic: already borrow…panic: attempt to subtract with overflow — Rust: thread 'main' panicked at src/main.rs:4:13: attempt to subtract with overflow (Rust)panic: attempt to sub…panic: called Option::unwrap() on a None value — Rust: thread 'main' panicked at src/main.rs:4:20: called `Option::unwrap()` on a `None` value (Rust)panic: called Option:…panic: called Result::unwrap() on an Err value — Rust: thread 'main' panicked at src/main.rs:5:36: called `Result::unwrap()` on an `Err` value: ParseIntError { kind: InvalidDigit } (Rust)panic: called Result:…panic: index out of bounds: the len is N but the index is M — Rust: thread 'main' panicked at src/main.rs:5:5: index out of bounds: the len is 3 but the index is 3 (Rust)panic: index out of b…panic: runtime error: index out of range — Go: panic: runtime error: index out of range [3] with length 3 (Go)panic: runtime error:…panic: runtime error: integer divide by zero — Go: panic: runtime error: integer divide by zero (Go)panic: runtime error:…panic: slice bounds out of range — Go: panic: runtime error: slice bounds out of range [:5] with capacity 3 (Go)panic: slice bounds o…panic: at…

Explore the map of this error Full error map →

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.

💬 What worked for others

No tips yet — did something else fix this for you?

Share what worked for you

Tips are checked by an editor before they appear. Links are removed.