Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ensure trait bounds within a trait?

Tags:

rust

If I have a trait like the following Foo trait

trait Foo: Bar {
  type Account: SomeTrait;
}

That has a trait bound that requires Bar

trait Bar {
  type UserId;
}

How can I request inside the Foo trait that this trait requires the UserId type inside the Bar trait to be u32?

Important note: I can't edit the Bar trait.

like image 785
Szegoo Avatar asked Oct 30 '25 21:10

Szegoo


1 Answers

This can be specified as a named generic argument: Bar<UserId=u32>.

trait SomeTrait {}

trait Foo: Bar<UserId=u32> {
    type Account: SomeTrait;
}

trait Bar {
    type UserId;
}

See the playground.

like image 120
BlackBeans Avatar answered Nov 01 '25 12:11

BlackBeans



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!