Advertisement
MaoChessy

anon func in struct

Jul 2nd, 2022
742
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 0.75 KB | None
  1. fn main() {
  2.     let elf = dev::Developer::new_elf_dev("Legolas".to_string());
  3.     let human = dev::Developer::new_human_dev("Not hobit".to_string());
  4.     elf.Log();
  5.     human.Log();
  6. }
  7.  
  8. mod dev {
  9.     pub struct Developer {
  10.         pub name: String,
  11.         pub get_age: fn() -> i32,
  12.     }
  13.  
  14.     impl Developer {
  15.         pub fn Log(&self) {
  16.             println!("{}", self.name)
  17.         }
  18.  
  19.         pub fn new_human_dev(s: String) -> Developer {
  20.             Developer {
  21.                 name: s,
  22.                 get_age: || -> i32 { 18 },
  23.             }
  24.         }
  25.  
  26.         pub fn new_elf_dev(s: String) -> Developer {
  27.             Developer {
  28.                 name: s,
  29.                 get_age: || -> i32 { 945 },
  30.             }
  31.         }
  32.     }
  33. }
  34.  
Advertisement
RAW Paste Data Copied
Advertisement