pub trait App {
fn run(&mut self);
}
#[derive(Clone, Copy)]
pub struct Cda{
d:i32,
}
impl Cda {
fn new(num:i32)->Self{
Self { d: num }
}
fn inc(&mut self)->Self{
self.d+=1;
*self
}
fn show(&self){
println!("run:{}",self.d);
}
}
impl App for Cda {
fn run(&mut self) {
self.inc().show();
}
}
pub type AppCreator = Box<dyn FnMut(i32) -> Box<dyn App>>;
fn runapp(cc:&mut AppCreator){
cc(22).run();
}
fn main(){
let mut cc:AppCreator =Box::new(|cc|Box::new(Cda::new(cc)));
runapp(&mut cc);
runapp(&mut cc);
}
标签:Box,分发,闭包加,mut,cc,self,fn,new,Rust
From: https://www.cnblogs.com/hardfood/p/17525523.html