Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
This error doesn't do a good job at all of highlighting the important thing! You can't define an inherent impl on a type parameter. You have to make it the impl of some trait. Here's an example: Tr...
Answer
#4: Post edited
- This error doesn't do a good job at all of highlighting the important thing!
You can't define an inherent `impl` on a type parameter. You have to make it the `impl` of some trait. Here's an example: [Try it online!](https://tio.run/##jZFLa8JAFIX3@RVHCmWCQfqw1o4aiKmLLgTBZQgS46QEklTnEfrA357OTNRqF6V3cbnMnPudkwlXQjaN5EkuEVAs80@2wZcDXfJjyzAd2TmrkJGS4rpUEtGSFVnsYa31eqJ06o6cveMcKGKu5Oxdjl@qinGKwL/EWpTlCL1sKa3yiMnLbXFa9jCnLXIc2bPYR7fl@b@sfGRvHPN/2BwkpgJKM2IUvUSstJq4Wura670JcxVtGM9rRhYJl3lSzHYentlavbqxIyRXqUQIogZ9nd0ER2BThBdviAnU8O@HVMPzVAaRIq9Qnh2aSns36E6wRiKgTUeny/1PZG1RJnlFjsCCSRi3WseoWdqJQnKrvzIkd7bf29534xZW9zLy4LZzIgTjcsV2HVJ70HsDq320fWj7kxubv9Y03w "Rust – Try It Online")- ```rust
- trait A: Sized {
- type B;
- fn f(m: &mut [Self], b: Self::B);
- }
trait AsMutExt<Inner: A>: Sized {- fn f(&mut self, b: Inner::B);
- }
impl<Inner: A, M: AsMut<[Inner]> + Sized> AsMutExt<Inner> for M {- fn f(&mut self, b: Inner::B) {
- A::f(self.as_mut(), b)
- }
- }
- #[derive(PartialEq, Debug)]
- struct C (u64);
- impl A for C {
- type B = u8;
- fn f(m: &mut [Self], b: u8) {
- for c in m {
- c.0 += b as u64;
- }
- }
- }
- fn main() {
- let mut v = vec![C(1), C(2), C(3), C(4)];
- v.f(5);
- assert_eq!(v, [C(6), C(7), C(8), C(9)]);
- }
- ```
- This error doesn't do a good job at all of highlighting the important thing!
- You can't define an inherent `impl` on a type parameter. You have to make it the `impl` of some trait. Here's an example: [Try it online!](https://tio.run/##jZFLa8JAFIX3@RVHCmWGBunT2lEDMXXRhVBwGYLEOCmBJNV5hD7wt6czE7W2i9K7uFxmzvnuyURoqdpWibRQCBkWxQdf49ODKfW@4ZiO3JzXyEnFcF5phXjByzzxsTJ6MzE2pSNv53l7ipxrNXtT46e65oIhDPY8x3AAaVzO3kkO/qLalEeXjznrWOPYnSVB8IsdIH8VmP8Dv5fYChnLiVX0U7k0akKNlLrrnQ1xFq@5KBpOnlOhirScbX088pV@oYknldCZQgSiB7cmsw2M0KWIfjwaJtDDv19OD09TWUSGokZ1cmgr61/iYoIVUgmzdHS83H1HNiuqtKjJAVhyBbutMTEanvXiiFyZr4zItes3rt/SpIM1/Zzc0W5OpeRCLfm2Rxofxjdw2nvXh64/0MT@rbb9Ag "Rust – Try It Online")
- ```rust
- trait A: Sized {
- type B;
- fn f(m: &mut [Self], b: Self::B);
- }
- trait AsMutExt<Inner: A> {
- fn f(&mut self, b: Inner::B);
- }
- impl<Inner: A, M: AsMut<[Inner]>> AsMutExt<Inner> for M {
- fn f(&mut self, b: Inner::B) {
- A::f(self.as_mut(), b)
- }
- }
- #[derive(PartialEq, Debug)]
- struct C (u64);
- impl A for C {
- type B = u8;
- fn f(m: &mut [Self], b: u8) {
- for c in m {
- c.0 += b as u64;
- }
- }
- }
- fn main() {
- let mut v = vec![C(1), C(2), C(3), C(4)];
- v.f(5);
- assert_eq!(v, [C(6), C(7), C(8), C(9)]);
- }
- ```
#3: Post edited
- This error doesn't do a good job at all of highlighting the important thing!
- You can't define an inherent `impl` on a type parameter. You have to make it the `impl` of some trait. Here's an example: [Try it online!](https://tio.run/##jZFLa8JAFIX3@RVHCmWCQfqw1o4aiKmLLgTBZQgS46QEklTnEfrA357OTNRqF6V3cbnMnPudkwlXQjaN5EkuEVAs80@2wZcDXfJjyzAd2TmrkJGS4rpUEtGSFVnsYa31eqJ06o6cveMcKGKu5Oxdjl@qinGKwL/EWpTlCL1sKa3yiMnLbXFa9jCnLXIc2bPYR7fl@b@sfGRvHPN/2BwkpgJKM2IUvUSstJq4Wura670JcxVtGM9rRhYJl3lSzHYentlavbqxIyRXqUQIogZ9nd0ER2BThBdviAnU8O@HVMPzVAaRIq9Qnh2aSns36E6wRiKgTUeny/1PZG1RJnlFjsCCSRi3WseoWdqJQnKrvzIkd7bf29534xZW9zLy4LZzIgTjcsV2HVJ70HsDq320fWj7kxubv9Y03w "Rust – Try It Online")
- ```rust
- trait A: Sized {
- type B;
- fn f(m: &mut [Self], b: Self::B);
- }
- trait AsMutExt<Inner: A>: Sized {
- fn f(&mut self, b: Inner::B);
- }
impl<Inner: A, M: AsMut<[Inner]> + Sized> M {- fn f(&mut self, b: Inner::B) {
- A::f(self.as_mut(), b)
- }
- }
- #[derive(PartialEq, Debug)]
- struct C (u64);
- impl A for C {
- type B = u8;
- fn f(m: &mut [Self], b: u8) {
- for c in m {
- c.0 += b as u64;
- }
- }
- }
- fn main() {
- let mut v = vec![C(1), C(2), C(3), C(4)];
- v.f(5);
- assert_eq!(v, [C(6), C(7), C(8), C(9)]);
- }
- ```
- This error doesn't do a good job at all of highlighting the important thing!
- You can't define an inherent `impl` on a type parameter. You have to make it the `impl` of some trait. Here's an example: [Try it online!](https://tio.run/##jZFLa8JAFIX3@RVHCmWCQfqw1o4aiKmLLgTBZQgS46QEklTnEfrA357OTNRqF6V3cbnMnPudkwlXQjaN5EkuEVAs80@2wZcDXfJjyzAd2TmrkJGS4rpUEtGSFVnsYa31eqJ06o6cveMcKGKu5Oxdjl@qinGKwL/EWpTlCL1sKa3yiMnLbXFa9jCnLXIc2bPYR7fl@b@sfGRvHPN/2BwkpgJKM2IUvUSstJq4Wura670JcxVtGM9rRhYJl3lSzHYentlavbqxIyRXqUQIogZ9nd0ER2BThBdviAnU8O@HVMPzVAaRIq9Qnh2aSns36E6wRiKgTUeny/1PZG1RJnlFjsCCSRi3WseoWdqJQnKrvzIkd7bf29534xZW9zLy4LZzIgTjcsV2HVJ70HsDq320fWj7kxubv9Y03w "Rust – Try It Online")
- ```rust
- trait A: Sized {
- type B;
- fn f(m: &mut [Self], b: Self::B);
- }
- trait AsMutExt<Inner: A>: Sized {
- fn f(&mut self, b: Inner::B);
- }
- impl<Inner: A, M: AsMut<[Inner]> + Sized> AsMutExt<Inner> for M {
- fn f(&mut self, b: Inner::B) {
- A::f(self.as_mut(), b)
- }
- }
- #[derive(PartialEq, Debug)]
- struct C (u64);
- impl A for C {
- type B = u8;
- fn f(m: &mut [Self], b: u8) {
- for c in m {
- c.0 += b as u64;
- }
- }
- }
- fn main() {
- let mut v = vec![C(1), C(2), C(3), C(4)];
- v.f(5);
- assert_eq!(v, [C(6), C(7), C(8), C(9)]);
- }
- ```
#2: Post edited
- This error doesn't do a good job at all of highlighting the important thing!
You can't define an inherent `impl` on a type parameter. You have to make it the `impl` of some trait. Here's an example: [Try it online!](https://tio.run/##jZFLa8JAFIX3@RVHCmWCQfqw1o4aiKmLLgTBZQgS46QEklTnEfrA357OTNRqF6V3cbnMnPudkwlXQjaN5EkuEVAs80@2wZcDXfJjyzAd2TmrkJGS4rpUEtGSFVnsYa31eqJ06o6cveMcKGKu5Oxdjl@qinGKwL/EWpTlCL1sKa3yiMnLbXFa9jCnLXIc2bPYR7fl@b@sfGRvHPN/2BwkpgJKM2IUvUSstJq4Wura670JcxVtGM9rRhYJl3lSzHYentlavbqxIyRXqUQIogZ9nd0ER2BThBdviAnU8O@HVMPzVAaRIq9Qnh2aSns36E6wRiKgTUeny/1PZG1RJnlFjsCCSRi3WseoWdqJQnKrvzIkd7bf29534xZW9zLy4LZzIgTjcsV2HVJ70HsDq320fWj7kxubv9Y03w "Rust – Try It Online")
- This error doesn't do a good job at all of highlighting the important thing!
- You can't define an inherent `impl` on a type parameter. You have to make it the `impl` of some trait. Here's an example: [Try it online!](https://tio.run/##jZFLa8JAFIX3@RVHCmWCQfqw1o4aiKmLLgTBZQgS46QEklTnEfrA357OTNRqF6V3cbnMnPudkwlXQjaN5EkuEVAs80@2wZcDXfJjyzAd2TmrkJGS4rpUEtGSFVnsYa31eqJ06o6cveMcKGKu5Oxdjl@qinGKwL/EWpTlCL1sKa3yiMnLbXFa9jCnLXIc2bPYR7fl@b@sfGRvHPN/2BwkpgJKM2IUvUSstJq4Wura670JcxVtGM9rRhYJl3lSzHYentlavbqxIyRXqUQIogZ9nd0ER2BThBdviAnU8O@HVMPzVAaRIq9Qnh2aSns36E6wRiKgTUeny/1PZG1RJnlFjsCCSRi3WseoWdqJQnKrvzIkd7bf29534xZW9zLy4LZzIgTjcsV2HVJ70HsDq320fWj7kxubv9Y03w "Rust – Try It Online")
- ```rust
- trait A: Sized {
- type B;
- fn f(m: &mut [Self], b: Self::B);
- }
- trait AsMutExt<Inner: A>: Sized {
- fn f(&mut self, b: Inner::B);
- }
- impl<Inner: A, M: AsMut<[Inner]> + Sized> M {
- fn f(&mut self, b: Inner::B) {
- A::f(self.as_mut(), b)
- }
- }
- #[derive(PartialEq, Debug)]
- struct C (u64);
- impl A for C {
- type B = u8;
- fn f(m: &mut [Self], b: u8) {
- for c in m {
- c.0 += b as u64;
- }
- }
- }
- fn main() {
- let mut v = vec![C(1), C(2), C(3), C(4)];
- v.f(5);
- assert_eq!(v, [C(6), C(7), C(8), C(9)]);
- }
- ```
#1: Initial revision
This error doesn't do a good job at all of highlighting the important thing! You can't define an inherent `impl` on a type parameter. You have to make it the `impl` of some trait. Here's an example: [Try it online!](https://tio.run/##jZFLa8JAFIX3@RVHCmWCQfqw1o4aiKmLLgTBZQgS46QEklTnEfrA357OTNRqF6V3cbnMnPudkwlXQjaN5EkuEVAs80@2wZcDXfJjyzAd2TmrkJGS4rpUEtGSFVnsYa31eqJ06o6cveMcKGKu5Oxdjl@qinGKwL/EWpTlCL1sKa3yiMnLbXFa9jCnLXIc2bPYR7fl@b@sfGRvHPN/2BwkpgJKM2IUvUSstJq4Wura670JcxVtGM9rRhYJl3lSzHYentlavbqxIyRXqUQIogZ9nd0ER2BThBdviAnU8O@HVMPzVAaRIq9Qnh2aSns36E6wRiKgTUeny/1PZG1RJnlFjsCCSRi3WseoWdqJQnKrvzIkd7bf29534xZW9zLy4LZzIgTjcsV2HVJ70HsDq320fWj7kxubv9Y03w "Rust – Try It Online")