浏览代码

Finished task2 until Restructuring

Joshua Rutschmann 8 年前
父节点
当前提交
49bbe8f530
共有 2 个文件被更改,包括 75 次插入0 次删除
  1. 6
    0
      hw2/task2/Cargo.toml
  2. 69
    0
      hw2/task2/src/main.rs

+ 6
- 0
hw2/task2/Cargo.toml 查看文件

@@ -0,0 +1,6 @@
1
+[package]
2
+name = "task2"
3
+version = "0.1.0"
4
+authors = ["Joshua Rutschmann <joshua.rutschmann@gmx.de>"]
5
+
6
+[dependencies]

+ 69
- 0
hw2/task2/src/main.rs 查看文件

@@ -0,0 +1,69 @@
1
+use std::env;
2
+use std::process;
3
+
4
+/// a struct to hold all of our configuration
5
+#[derive(Debug,PartialEq)]
6
+struct Config{
7
+    search: char,
8
+    line: String,
9
+}
10
+
11
+fn main() {
12
+    let args = env::args().collect();
13
+    print_arguments(&args);
14
+    //let conf = parse_arguments_simple(&args);
15
+    let res = parse_arguments(&args);
16
+    match res {
17
+        Ok(conf) => println!("{:?}", conf),
18
+        Err(_) => process::exit(1),
19
+    }
20
+
21
+}
22
+
23
+fn run() {
24
+
25
+}
26
+
27
+/// Parses relevant arguments, returning the filled Config in Result
28
+///
29
+///
30
+/// This function will parse the relevant arguments from the
31
+/// given <Strings>.
32
+/// Returns Config or Error Message in Result
33
+fn parse_arguments(args: &Vec<String>) -> Result<Config, String> {
34
+
35
+    if args.len() < 3 {
36
+        return Err("not ennugh parameters".to_string());
37
+    }
38
+
39
+    if args[1].chars().nth(0) == None {
40
+        return Err("char mismatch".to_string());    
41
+    }
42
+
43
+    let config = Config{search: args[1].chars().nth(0).unwrap(), line: args[2].clone()};
44
+    Ok(config)
45
+}
46
+
47
+/// Parses relevant arguments, returning the filled Config
48
+///
49
+///
50
+/// This function will parse the relevant arguments from the
51
+/// given <Strings>.
52
+/// Returns Config
53
+#[allow(dead_code)]
54
+fn parse_arguments_simple(args: &Vec<String>) -> Config {
55
+    Config{search: args[1].chars().nth(0).unwrap(), line: args[2].clone()}
56
+}
57
+
58
+/// Prints elements of Vec
59
+///
60
+///
61
+/// This function will print all elements of Vec with "args found: <elem>" in
62
+/// each line
63
+///
64
+/// Returns nothing
65
+fn print_arguments(args: &Vec<String>) {
66
+    for s in args {
67
+        println!("args found: {}", s);
68
+    }
69
+}

正在加载...
取消
保存