Sfoglia il codice sorgente

Solved conflict. Pipe working.

themultiplexer 8 anni fa
parent
commit
9b79795c3f
1 ha cambiato i file con 31 aggiunte e 8 eliminazioni
  1. 31
    8
      hw6/task1/src/main.rs

+ 31
- 8
hw6/task1/src/main.rs Vedi File

5
 
5
 
6
 use nix::unistd::{fork, getpid, pipe, read, write};
6
 use nix::unistd::{fork, getpid, pipe, read, write};
7
 use nix::sys::wait::wait;
7
 use nix::sys::wait::wait;
8
+use std::str;
8
 use nix::sys::wait::WaitStatus;
9
 use nix::sys::wait::WaitStatus;
9
 use nix::unistd::ForkResult::{Child, Parent};
10
 use nix::unistd::ForkResult::{Child, Parent};
10
 
11
 
11
 fn main() {
12
 fn main() {
12
 
13
 
13
- let arguments: Vec<String> = args().collect();
14
+    let arguments: Vec<String> = args().collect();
14
 
15
 
15
     if arguments.len() > 2 {
16
     if arguments.len() > 2 {
17
+
16
         let (reader, writer) = pipe().unwrap();
18
         let (reader, writer) = pipe().unwrap();
17
         let msg = "hello from parent";
19
         let msg = "hello from parent";
18
         let pid = fork();
20
         let pid = fork();
49
 }
51
 }
50
 
52
 
51
 /// Concats the two given String *references* and returns them as a String.
53
 /// Concats the two given String *references* and returns them as a String.
52
-fn concatenate_strings<'a>(s1:&'a str, s2:&'a str) -> String {
54
+fn concatenate_strings<'a>(s1: &'a str, s2: &'a str) -> String {
53
     s1.to_string() + s2
55
     s1.to_string() + s2
54
 }
56
 }
55
 
57
 
56
-/// Splits the given String *reference* and returns it as Vector of Strings.
57
-fn split_into_strings<'a>(s1:&'a str) -> Vec<String> {
58
+/// Splits the given String reference and returns it as Vector.
59
+/*fn split_into_strings<'2>(s1:&'a str) -> Vec<&'a str> {
58
     s1.to_string().split_whitespace().collect()
60
     s1.to_string().split_whitespace().collect()
61
+}*/
62
+/// Calculates the sum of the given Strings and returns them as a Result.
63
+fn sum_strings(v: Vec<&str>) -> Result<i32, &str> {
64
+    let mut sum = 0;
65
+    for x in v {
66
+        match x.parse::<i32>() {
67
+            Ok(val) => {
68
+                sum += val;
69
+            }
70
+            Err(_) => return Err("Given String is not a int"),
71
+        }
72
+    }
73
+    Ok(sum)
59
 }
74
 }
60
 
75
 
61
-fn sum_strings(){
62
-}
63
-
64
-fn mul_strings(){
76
+/// Calculates the product of the given Strings and returns them as a Result.
77
+fn mul_strings(v: Vec<&str>) -> Result<i32, &str> {
78
+    let mut prod = 0;
79
+    for x in v {
80
+        match x.parse::<i32>() {
81
+            Ok(val) => {
82
+                prod *= val;
83
+            }
84
+            Err(_) => return Err("Given String is not a int"),
85
+        }
86
+    }
87
+    Ok(prod)
65
 }
88
 }

Loading…
Annulla
Salva