Advertisement
fcamuso

Un assaggio di Rust: video 9

Jan 17th, 2023
1,262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.78 KB | None | 0 0
  1. //main.rs
  2.  
  3. // pub struct Utente {
  4. //   attivo: bool,
  5. //   username: String,
  6. //   email: String,
  7. // }
  8. mod modulo;
  9.  
  10. trait Contattabile {
  11.   fn get_contact(&self) -> String;
  12. }
  13.  
  14. fn main() {
  15.  
  16.   // let mut _utente1 = modulo::Utente {
  17.   //   username: String::from("fcamuso"),
  18.   //   email: String::from("fcamuso@gmail.com"),
  19.   //   attivo: true,  
  20.   // };
  21.  
  22.   let mut _utente1 = modulo::Utente::new(
  23.       String::from("fcamuso"),
  24.       true,
  25.       String::from("fcamuso@gmail.com")
  26.   );
  27.  
  28.   println!("{}",_utente1.get_user());
  29.  
  30.   // _utente1.email = String::from("fcamuso@mailg.com");
  31.   // println!("{}", _utente1.email);
  32.  
  33.   let mut _ditta1 = modulo::Ditta::new(
  34.     String::from("ACME spa"),
  35.     String::from("acmespa@gmail.com")
  36.   );
  37.  
  38.   let mut _contatti: Vec<Box<dyn Contattabile>> =
  39.     vec![Box::new(_utente1), Box::new(_ditta1)];
  40.  
  41.   for contatto in _contatti {
  42.     println!("{}", contatto.get_contact());
  43.   }
  44.  
  45.  
  46. }
  47. // ------------------------------------------------------------------------
  48. //modulo.rs
  49. use crate::Contattabile;
  50.  
  51. pub struct Utente {
  52.   username: String,
  53.   attivo: bool,
  54.   email: String,
  55. }
  56.  
  57. impl Contattabile for Utente
  58. {
  59.   fn get_contact(&self) -> String {
  60.       self.email.clone()
  61.   }
  62. }
  63.  
  64. impl Utente {
  65.  
  66.   pub fn new(username: String, attivo: bool, email: String ) -> Self {
  67.     Self { username, attivo, email }
  68.   }
  69.  
  70.   pub fn get_user(&self) -> String {
  71.     self.username.clone()
  72.   }
  73.  
  74. }
  75.  
  76. pub struct Ditta {
  77.   ragione_sociale: String,
  78.   indirizzo_mail: String
  79. }
  80.  
  81. impl Ditta {
  82.   pub fn new(ragione_sociale: String, indirizzo_mail: String ) -> Self {
  83.     Self { ragione_sociale, indirizzo_mail }
  84.   }
  85. }
  86. impl Contattabile for Ditta
  87. {
  88.   fn get_contact(&self) -> String {
  89.       self.indirizzo_mail.clone()
  90.   }
  91. }
  92.  
  93.  
  94.    
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement