forked from CocDap/Rust-Bootcamp-2023
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
130 lines (103 loc) · 3 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Exercise 1
// Make it compile
fn exercise1() {
// Use as many approaches as you can to make it work
let x = String::from("hello, world");
let y = &x;
let z = &x;
}
// Exercise 2
// Make it compile
// Don't modify code in exercise2 function!
fn exercise2() {
let s1 = String::from("hello, world");
let s2 = take_ownership(s1);
println!("{}", s2);
}
// Only modify the code below!
fn take_ownership(s: String) -> String {
s
}
// Exercise 3
// Make it compile
// Dont care about logic
fn exercise3() {
let values: Vec<f64> = vec![
2817.42, 2162.17, 3756.57, 2817.42, -2817.42, 946.9, 2817.42, 964.42, 795.43, 3756.57,
139.34, 903.58, -3756.57, 939.14, 828.04, 1120.04, 604.03, 3354.74, 2748.06, 1470.8,
4695.71, 71.11, 2391.48, 331.29, 1214.69, 863.52, 7810.01,
];
let values_number = values.len();
let additions: Vec<usize> = vec![0];
println!("{:?}", values_number);
while additions.len() > 0 {
let mut addition: f64 = 0.0;
// Sumar valores en additions
for &element_index in &additions {
let addition_aux = values[element_index];
addition = addition_aux + addition;
}
}
}
// Exercise 4
// Make it compile
fn exercise4(value: u32) -> String {
let str_value = value.to_string();
str_value
}
// Exercise 5
// Make it compile
use std::collections::HashMap;
fn exercise5() {
let mut my_map = HashMap::from([(1, "1.0".to_string()), (2, "2.0".to_string())]);
let key = 3;
let res = match my_map.get(&key) {
Some(child) => child,
None => {
let value = "3.0".to_string();
my_map.insert(key, value);
&my_map[&key] // HERE IT FAILS
}
};
println!("{}", res);
}
// Exercise 6
// Make it compile
use std::io;
fn exercise6() {
let mut prev_key: String = String::from("");
for line in io::stdin().lines() {
let s = line.unwrap();
let data: Vec<&str> = s.split("\t").collect();
if prev_key.len() == 0 {
prev_key = data[0].to_string();
}
}
}
// Exercise 7
// Make it compile
fn exercise7() {
let mut v: Vec<&str> = Vec::new();
let chars = [b'x', b'y', b'z'];
let s: &str = std::str::from_utf8(&chars).unwrap();
v.push(&s);
println!("{:?}", v);
}
// Exercise 8
// Make it compile
fn exercise8() {
let mut accounting = vec!["Alice".to_string(), "Ben".to_string()];
loop {
let mut add_input = String::from("");
io::stdin()
.read_line(&mut add_input)
.expect("Failed to read line");
let add_vec: Vec<&str> = add_input.trim()[..].split_whitespace().collect();
if add_vec.len() < 1 {
println!("Incorrect input, try again");
continue;
}
let person = add_vec[0];
accounting.push(person.to_string());
}
}