E0116
Rust Programming Language
Severity: MinorWhat it means
rustc: error[E0116]: cannot define inherent `impl` for a type outside of the crate where the type is defined.
Rust's error index: an inherent implementation, impl Type { ... } with no trait, is only allowed in the crate where the type was defined. impl Vec
A type alias doesn't get around it: type Bytes = Vec
Affected Models
- rustc and cargo, all Rust editions
How to Fix It
-
Define a trait with the methods you want, and implement it for the type: trait ByteSum { fn total(&self) -> u32; } and impl ByteSum for Vec<u8> { ... }.
Bring the trait into scope with use wherever you call its methods.
-
Or wrap the type in a struct of your own, a newtype, and write the impl for that: struct Bytes(Vec<u8>); impl Bytes { ... }.