Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct Calculator<C>
- where C: Fn(u32, u32) -> u32
- {
- context: (u32, u32),
- calculate: C,
- }
- impl<C> Calculator<C>
- where C: Fn(u32, u32) -> u32
- {
- fn new(lhs: u32, rhs: u32, calculate: C) -> Self
- {
- Self { context: (lhs, rhs), calculate }
- }
- fn execute(&self) -> u32
- {
- let (lhs, rhs) = self.context;
- (self.calculate)(lhs, rhs)
- }
- }
- fn main()
- {
- let c1 = Calculator::new(12, 33, |l, r| l + r);
- let c2 = Calculator::new(3, 5, |l, r| l*r);
- let x = 12;
- let c3 = Calculator::new(66, 4, |l, r| l + x + r);
- dbg!(c1.execute());
- dbg!(c2.execute());
- dbg!(c3.execute());
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement