Quellcode durchsuchen

Completed functions and added comments. Rustfmt.

Lorenz Bung vor 8 Jahren
Ursprung
Commit
934bc2cd4d
1 geänderte Dateien mit 41 neuen und 22 gelöschten Zeilen
  1. 41
    22
      hw6/task1/src/main.rs

+ 41
- 22
hw6/task1/src/main.rs Datei anzeigen

@@ -10,27 +10,25 @@ use nix::unistd::ForkResult::{Child, Parent};
10 10
 
11 11
 fn main() {
12 12
 
13
- let arguments: Vec<String> = args().collect();
13
+    let arguments: Vec<String> = args().collect();
14 14
 
15
-    if arguments.len() != 0 {
16
-   
15
+    if arguments.len() > 2 {
17 16
 
18
-    let pid = fork();
19
-    match pid {
20
-        Ok(Child) => {
21
-            println!("hello, I am child (pid:{})", getpid());
22
-        }
23 17
 
24
-        Ok(Parent { child }) => {
25
-            if let Ok(ws) = wait() {
26
-                if let WaitStatus::Exited(child_pid, exit_code) = ws {
27
-                
18
+        let pid = fork();
19
+        match pid {
20
+            Ok(Child) => {
21
+                println!("hello, I am child (pid:{})", getpid());
22
+            }
23
+
24
+            Ok(Parent { child }) => {
25
+                if let Ok(ws) = wait() {
26
+                    if let WaitStatus::Exited(child_pid, exit_code) = ws {}
28 27
                 }
29 28
             }
30
-        }
31 29
 
32
-        Err(_) => {}
33
-    }
30
+            Err(_) => {}
31
+        }
34 32
 
35 33
 
36 34
 
@@ -42,17 +40,38 @@ fn main() {
42 40
 }
43 41
 
44 42
 /// Concats the two given String *references* and returns them as a String.
45
-fn concatenate_strings<'a>(s1:&'a str, s2:&'a str) -> String {
43
+fn concatenate_strings<'a>(s1: &'a str, s2: &'a str) -> String {
46 44
     s1.to_string() + s2
47 45
 }
48 46
 
49
-/// Splits the given String *reference* and returns it as Vector of Strings.
50
-fn split_into_strings<'a>(s1:&'a str) -> Vec<String> {
47
+/// Splits the given String reference and returns it as Vector.
48
+/*fn split_into_strings<'2>(s1:&'a str) -> Vec<&'a str> {
51 49
     s1.to_string().split_whitespace().collect()
50
+}*/
51
+/// Calculates the sum of the given Strings and returns them as a Result.
52
+fn sum_strings(v: Vec<&str>) -> Result<i32, &str> {
53
+    let mut sum = 0;
54
+    for x in v {
55
+        match x.parse::<i32>() {
56
+            Ok(val) => {
57
+                sum += val;
58
+            }
59
+            Err(_) => return Err("Given String is not a int"),
60
+        }
61
+    }
62
+    Ok(sum)
52 63
 }
53 64
 
54
-fn sum_strings(){
55
-}
56
-
57
-fn mul_strings(){
65
+/// Calculates the product of the given Strings and returns them as a Result.
66
+fn mul_strings(v: Vec<&str>) -> Result<i32, &str> {
67
+    let mut prod = 0;
68
+    for x in v {
69
+        match x.parse::<i32>() {
70
+            Ok(val) => {
71
+                prod *= val;
72
+            }
73
+            Err(_) => return Err("Given String is not a int"),
74
+        }
75
+    }
76
+    Ok(prod)
58 77
 }

Laden…
Abbrechen
Speichern