Rust errors / Types and traits
Rust compiler error · checked with rustc 1.99.0
Rust error E0197: inherent impls cannot be unsafe
What it meansAn inherent implementation was marked unsafe.
What rustc prints
error[E0197]: inherent impls cannot be unsafe --> main.rs:3:13 | 3 | unsafe impl Foo { } // error! | ------ ^^^ inherent impl for this type | | | unsafe because of this | = note: only trait implementations may be annotated with `unsafe`
Real output of rustc 1.99.0 compiling the official example below (2021 edition). Your names and line numbers will differ; the first line is the part to match.
The compiler team's explanation
An inherent implementation was marked unsafe.
Erroneous code example:
struct Foo;
unsafe impl Foo { } // error!
Inherent implementations (one that do not implement a trait but provide methods associated with a type) are always safe because they are not implementing an unsafe trait. Removing the unsafe keyword from the inherent implementation will resolve this error.
struct Foo;
impl Foo { } // ok!From rustc --explain E0197 (1.99.0), part of the Rust project, MIT or Apache-2.0. Official page.
Related errors
- E0308mismatched types
- E0277the trait bound is not satisfied
- E0599no method named X found in the current scope
- E0433failed to resolve
- E0186method `foo` has a `&self` declaration in the trait, but not in the impl
- E0191the value of the associated type `Bar` in `Trait` must be specified
- E0198negative impls cannot be unsafe
- E0199implementing the trait `Bar` is not unsafe
Up next
Often hit nextE0308: mismatched typesA related error from the same family.Read it next →Did this answer your question?