How to specify "can convert to" constraints in generic parameters?

I am attempting to define a trait with generic parameters A,B, and I wish the trait to know that A must be a type which is convertible into B. (Specifically, I will define structs A,B myself and can implement a conversion logic). But how do I specify this in the trait definition? I looked into trait bounds but it does not seem to have the answer.

pub trait MyTrait<A,B>{ // I wanted to add a constraint saying "A must be convertible into B" 
   fn createA() -> A; 

   fn createB() -> B{
       let A = createA(); 
       // I wish to convert A into B and return an instance of B here. 




You can use the From and/or Into traits. For example:

pub trait MyTrait<A, B> where A: Into<B> {
   fn createA() -> A;

   fn createB() -> B {
       let a = Self::createA(); 

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.