diff --git a/crates/bevy_ecs/src/world/mod.rs b/crates/bevy_ecs/src/world/mod.rs index bd6c10e6a9079..0dc6a35f4ee51 100644 --- a/crates/bevy_ecs/src/world/mod.rs +++ b/crates/bevy_ecs/src/world/mod.rs @@ -186,7 +186,9 @@ impl World { /// ``` #[inline] pub fn entity(&self, entity: Entity) -> EntityRef { - self.get_entity(entity).expect("Entity does not exist") + // Lazily evaluate panic!() via unwrap_or_else() to avoid allocation unless failure + self.get_entity(entity) + .unwrap_or_else(|| panic!("Entity {:?} does not exist", entity)) } /// Retrieves an [EntityMut] that exposes read and write operations for the given `entity`. @@ -211,7 +213,9 @@ impl World { /// ``` #[inline] pub fn entity_mut(&mut self, entity: Entity) -> EntityMut { - self.get_entity_mut(entity).expect("Entity does not exist") + // Lazily evaluate panic!() via unwrap_or_else() to avoid allocation unless failure + self.get_entity_mut(entity) + .unwrap_or_else(|| panic!("Entity {:?} does not exist", entity)) } /// Retrieves an [EntityRef] that exposes read-only operations for the given `entity`.