Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

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

80%
+6 −0
Q&A How can I define a method on [&mut T] where T: MyTrait?

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...

posted 4y ago by r~~‭  ·  edited 4y ago by r~~‭

Answer
#4: Post edited by user avatar r~~‭ · 2020-09-20T04:35:47Z (about 4 years ago)
Sized is only necessary on A, not AsMutExt
  • 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 by user avatar wizzwizz4‭ · 2020-09-20T04:28:13Z (about 4 years ago)
  • 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 by user avatar r~~‭ · 2020-09-14T23:59:47Z (about 4 years ago)
  • 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 by user avatar r~~‭ · 2020-09-14T23:56:47Z (about 4 years ago)
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")