Rust errors / Types and traits
Rust compiler error · checked with rustc 1.99.0
Rust error E0198: negative impls cannot be unsafe
What it meansA negative implementation was marked as unsafe.
What rustc prints
error[E0198]: negative impls cannot be unsafe
--> main.rs:3:13
|
3 | unsafe impl !Clone for Foo { } // error!
| ------ -^^^^^
| | |
| | negative because of this
| unsafe because of thisReal 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
A negative implementation was marked as unsafe.
Erroneous code example:
struct Foo;
unsafe impl !Clone for Foo { } // error!
A negative implementation is one that excludes a type from implementing a particular trait. Not being able to use a trait is always a safe operation, so negative implementations are always safe and never need to be marked as unsafe.
This will compile:
#![feature(auto_traits)]
struct Foo;
auto trait Enterprise {}
impl !Enterprise for Foo { }
Please note that negative impls are only allowed for auto traits.
From rustc --explain E0198 (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
- E0191the value of the associated type `Bar` in `Trait` must be specified
- E0197inherent impls cannot be unsafe
- E0199implementing the trait `Bar` is not unsafe
- E0200the trait `Bar` requires an `unsafe impl` declaration
Up next
Often hit nextE0308: mismatched typesA related error from the same family.Read it next →Did this answer your question?