Parcourir la source

Solved conflict. Pipe working.

themultiplexer il y a 8 ans
Parent
révision
9b79795c3f
1 fichiers modifiés avec 31 ajouts et 8 suppressions
  1. 31
    8
      hw6/task1/src/main.rs

+ 31
- 8
hw6/task1/src/main.rs Voir le fichier

@@ -5,14 +5,16 @@ extern crate nix;
5 5
 
6 6
 use nix::unistd::{fork, getpid, pipe, read, write};
7 7
 use nix::sys::wait::wait;
8
+use std::str;
8 9
 use nix::sys::wait::WaitStatus;
9 10
 use nix::unistd::ForkResult::{Child, Parent};
10 11
 
11 12
 fn main() {
12 13
 
13
- let arguments: Vec<String> = args().collect();
14
+    let arguments: Vec<String> = args().collect();
14 15
 
15 16
     if arguments.len() > 2 {
17
+
16 18
         let (reader, writer) = pipe().unwrap();
17 19
         let msg = "hello from parent";
18 20
         let pid = fork();
@@ -49,17 +51,38 @@ fn main() {
49 51
 }
50 52
 
51 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 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 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
 }

Chargement…
Annuler
Enregistrer