Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. struct Config {
  2. query: String,
  3. filename: String,
  4. }
  5.  
  6. fn main() {
  7. let args: Vec<String> = env::args().collect();
  8. let config = Config::new(&args)
  9. }
  10.  
  11. impl Config {
  12. fn new(args: &[String]) -> Result<Config, &'static str> {
  13. // [...]
  14. let query = args[1].clone();
  15. let filename = args[2].clone();
  16. // [...]
  17. }
  18. }
  19.  
  20. fn main() {
  21. let config = Config::new(env::args())
  22. }
  23.  
  24. impl Config {
  25. fn new(mut args: std::env::Args) -> Result<Config, &'static str> {
  26. args.next();
  27.  
  28. let query = match args.next() {
  29. Some(arg) => arg,
  30. None => return Err("Didn't get a query string"),
  31. };
  32.  
  33. let filename = match args.next() {
  34. Some(arg) => arg,
  35. None => return Err("Didn't get a file name"),
  36. };
  37. // [...]
  38. }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement