Rust: thread 'main' panicked at src/main.rs:3:13: attempt to divide by zero
Integer division or remainder by zero.
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 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
a.checked_div(b).ok_or(Error::DivZero)?- Guard:
if b == 0 { return None; } - Use floats if infinity is acceptable
Detailed fix by platform
Rust
let avg = sum.checked_div(count).unwrap_or(0);
How to diagnose
- Divisor — Zero when?
- Source — Empty data?
- 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:
Similar errors
- panic: already borrowed: BorrowMutError Rust: thread 'main' panicked at src/main.rs:8:9: already borrowed: BorrowMutError / already mutably borrowed: BorrowError
- panic: attempt to subtract with overflow Rust: thread 'main' panicked at src/main.rs:4:13: attempt to subtract with overflow
- 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
Most viewed in Rust
Other ways to find it
Where this error sits
Origin: Application & API. Errors that are often confused with or caused by this one (click to open):
🧠 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.
Report a correction or suggest an improvement
Last updated 5 Nov 2026