Advertisement
Guest User

main.rs

a guest
May 11th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 1.15 KB | None | 0 0
  1. use std::env;
  2. use std::process;
  3.  
  4. struct Pessoa {
  5.     nome: String,
  6.     idade: u8,
  7. }
  8.  
  9. trait P {
  10.     fn new(nome: String, idade: u8) -> Pessoa;
  11.     fn show(&self);
  12. }
  13.  
  14. impl P for Pessoa {
  15.     fn new(nome: String, idade: u8) -> Pessoa {
  16.         Pessoa { nome, idade }
  17.     }
  18.     fn show(&self) {
  19.         println!("Nome: {}\nIdade: {}", self.nome, self.idade);
  20.     }
  21. }
  22.  
  23. fn main() {
  24.     let args: Vec<String> = env::args().collect();
  25.     let nome = &args[1].clone();
  26.     let idade = &args[2].clone().trim().parse::<u8>().unwrap();
  27.     // match args.len() {
  28.     //     2 => println!(
  29.     //         "Modo de Uso:\n\t $ {} <nome> <idade>\nEx:\t $ {} \"foobar\" 00",
  30.     //         args[0], args[0]
  31.     //     ),
  32.     //     _ => print!("erro"),
  33.     // };
  34.  
  35.     if args.len() < 3 {
  36.         println!(
  37.             "Modo de Uso:\n\t $ {} <nome> <idade>\nEx:\t $ {} \"foobar\" 00",
  38.             args[0], args[0]
  39.         );
  40.         process::exit(1); // eu quero quitar, o compilador acusa erro "thread 'main' panicked at 'index out of bounds: the len is 1 but the index is 1'"
  41.     }
  42.  
  43.     let p = Pessoa::new(nome.to_string(), *idade);
  44.     p.show();
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement