|
|
@@ -70,16 +70,25 @@ fn concatenate_strings<'a>(s1: &'a str, s2: &'a str) -> String {
|
|
70
|
70
|
}
|
|
71
|
71
|
|
|
72
|
72
|
/// Splits the given String reference and returns it as Vector.
|
|
73
|
|
-/*fn split_into_strings<'2>(s1:&'a str) -> Vec<&'a str> {
|
|
74
|
|
- s1.to_string().split_whitespace().collect()
|
|
75
|
|
-}*/
|
|
|
73
|
+fn split_into_strings<'a>(s1: &'a str) -> Vec<String> {
|
|
|
74
|
+ s1.to_string()
|
|
|
75
|
+ .split_whitespace()
|
|
|
76
|
+ .map(|s| s.to_string())
|
|
|
77
|
+ .collect()
|
|
|
78
|
+}
|
|
|
79
|
+
|
|
76
|
80
|
/// Calculates the sum of the given Strings and returns them as a Result.
|
|
77
|
81
|
fn sum_strings(v: Vec<&str>) -> Result<i32, &str> {
|
|
78
|
82
|
let mut sum = 0;
|
|
79
|
83
|
for x in v {
|
|
80
|
84
|
match x.parse::<i32>() {
|
|
81
|
85
|
Ok(val) => {
|
|
82
|
|
- sum += val;
|
|
|
86
|
+ match i32::checked_add(sum, val) {
|
|
|
87
|
+ Some(y) => {
|
|
|
88
|
+ sum = y;
|
|
|
89
|
+ }
|
|
|
90
|
+ None => return Err("Overflow would happen in sum_strings()"),
|
|
|
91
|
+ }
|
|
83
|
92
|
}
|
|
84
|
93
|
Err(_) => return Err("Given String is not a int"),
|
|
85
|
94
|
}
|
|
|
@@ -93,7 +102,12 @@ fn mul_strings(v: Vec<&str>) -> Result<i32, &str> {
|
|
93
|
102
|
for x in v {
|
|
94
|
103
|
match x.parse::<i32>() {
|
|
95
|
104
|
Ok(val) => {
|
|
96
|
|
- prod *= val;
|
|
|
105
|
+ match i32::checked_mul(prod, val) {
|
|
|
106
|
+ Some(y) => {
|
|
|
107
|
+ prod = y;
|
|
|
108
|
+ }
|
|
|
109
|
+ None => return Err("Overflow would happen in mul_strings()"),
|
|
|
110
|
+ }
|
|
97
|
111
|
}
|
|
98
|
112
|
Err(_) => return Err("Given String is not a int"),
|
|
99
|
113
|
}
|