Advertisement
fcamuso

Un assaggio di Rust: video 8

Jan 1st, 2023
1,551
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.10 KB | None | 0 0
  1. use std::fs::File;
  2.  
  3. // fn test1() -> i32 {
  4. //   5
  5. // }
  6.  
  7. // fn test2() {
  8. //   let x = 8;
  9. // }
  10.  
  11. // fn test3() -> i32 {
  12. //   let mut x=1;
  13. //   x*2;
  14. //   if x>3 {x=5}
  15. //   return x;
  16. // }
  17.  
  18. //ERRORI: recuperabili e irrecuperabili
  19.  
  20. //irecuperabili
  21. //  panic!  
  22.  
  23. //recuperabili
  24. /*
  25.   enum Result<T, E> {
  26.     Ok(T),
  27.     Err(E),
  28.   }
  29.  
  30. */
  31.  
  32. fn ha_requisiti_patente(eta: i32) -> Result<bool, String> {
  33.   if eta>=18 {
  34.     return Ok(true);
  35.   }
  36.   else {
  37.     return Err("Non è maggiorenne".to_string());
  38.   }
  39. }
  40.  
  41. fn main() {
  42.   //println!("{}", test1());
  43.   // //println!("{}", test2()); //NO test2 non restituisce nulla
  44.   // println!("{}", test3());
  45.   // //panic!("Addio mondo crudele!\n");
  46.   // test3();
  47.  
  48.   match File::open("d:\\dati.txt")
  49.   {
  50.     Ok(_file) => println!("File correttamente aperto"),
  51.     Err(e) => {
  52.       println!("Errore: {}", e);
  53.     }    
  54.   };
  55.  
  56.   match ha_requisiti_patente(21) {
  57.     Ok(_esito) => {println!("Può conseguire la patente\n");}
  58.     Err(violazioni) => {println!("Almeno uno dei requisiti non soddisfatto: {}", violazioni);}
  59.   }
  60.  
  61.  
  62. }
  63.  
  64.  
  65.  
  66.  
  67.    
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement