leptos-rs/leptos · error
called `unwrap_{variant}()` on a non-`{variant}` variant of
Error message
called `unwrap_{variant}()` on a non-`{variant}` variant of `{name}` What it means
Generated Either enum types provide typed unwrap_<variant>() methods; calling one on an instance holding a different variant panics with this message naming the expected variant and enum. It is a misuse-of-sum-type error: the caller assumed the wrong side of the either.
Source
Thrown at either_of/src/lib.rs:88
pub fn [<as_ $variant:lower>](&self) -> Option<&$ty> {
match self {
$name::$variant(inner) => Some(inner),
_ => None,
}
}
pub fn [<as_ $variant:lower _mut>](&mut self) -> Option<&mut $ty> {
match self {
$name::$variant(inner) => Some(inner),
_ => None,
}
}
pub fn [<unwrap_ $variant:lower>](self) -> $ty {
match self {
$name::$variant(inner) => inner,
_ => panic!(concat!(
"called `unwrap_", stringify!([<$variant:lower>]), "()` on a non-`", stringify!($variant), "` variant of `", stringify!($name), "`"
)),
}
}
pub fn [<into_ $variant:lower>](self) -> Result<$ty, Self> {
match self {
$name::$variant(inner) => Ok(inner),
_ => Err(self),
}
}
)+
}
}
impl<$($ty),+> Display for $name<$($ty),+>
where
$($ty: Display,)+View on GitHub (pinned to 32d20f6c9d)
Solutions
- Match on the enum instead of unwrapping, handling both variants explicitly.
- Use the generated into_<variant>() (returns Result) to attempt conversion safely.
- If you must unwrap, first check with the corresponding is_<variant>-style or pattern-match guard.
Example fix
// before
let value = either.unwrap_left(); // panics when it is Right
// after
let value = match either {
Either::Left(v) => v,
Either::Right(v) => v,
}; Defensive patterns
Strategy: type-guard
Type guard
fn as_left<T, R>(e: Either<T, R>) -> Option<T> {
match e { Either::Left(v) => Some(v), _ => None }
} Try / catch
// Rust panics are not catchable with try/catch; avoid via match:
match either {
Either::Left(v) => handle_left(v),
Either::Right(v) => handle_right(v),
} Prevention
- Never unwrap Either variants in view code; match both branches.
- Prefer into_left()/into_right() which return Result.
- If logic only applies to one variant, restructure so that variant is the only one produced.
When it happens
Trigger: Calling e.g. Either::Left(v).unwrap_right() (or unwrap_left on a Right) on a generated EitherOfN enum; the message is built by the macro with stringify!($variant)/stringify!($name).
Common situations: View rendering code where a conditional switched which branch of Either is produced (e.g. a show/if changed at runtime) and a downstream unwrap assumes the other branch; refactors that reorder enum variants.
Related errors
- Executor::spawn called, but no global 'spawn' function is co
- Executor::spawn_local called, but no global 'spawn_local' fu
- At {caller}, tried to spawn a Future with Executor::spawn()
- At {caller}, tried to spawn a Future with Executor::spawn_lo
- Failed to find the route {path} requested by the user. This
AI-assisted analysis of leptos-rs/leptos@32d20f6c9d (2026-09-01).
Data as JSON: /api/errors/a896018d17473718.
Report an issue: GitHub.