Move object from Box<dyn Trait<A>> to Box<dyn Trait<B>>
02:58 24 Aug 2026

I have a Parser struct that implements a generic Parse trait:

struct CsvParser;  
  
trait Parse {  
    fn parse(&mut self, source: S);  
}  
  
struct SourceA;  
struct SourceB;  
  
impl Parse for CsvParser {  
    fn parse(&mut self, _source: SourceA) {  
        // SourceA implementation  
    }  
}  
  
impl Parse for CsvParser {  
    fn parse(&mut self, _source: SourceB) {  
        // SourceB implementation  
    }  
}  

At callsite I instantiate it as a type erased object:

let mut parser: Box> = Box::new(CsvParser);  
parser.parse(SourceA);  

I now want to move the underlying CsvParser into a different box so that I can access a different parse method:

let mut parser2: Box> = /\* move from parser \*/;  
parser2.parse(SourceB);

How can I move the underlying CsvParser from Box> into Box> so that parser2.parse(SourceB) uses the Parse implementation?

generics rust traits