Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

done-exercises2 #20

Open
wants to merge 2 commits into
base: class-2-exercises
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions exercises/ownership-borrowing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
fn exercise1() {
// Use as many approaches as you can to make it work
let x = String::from("hello, world");
let y = x;
let y = &x;
let z = x;

}

// Exercise 2
Expand Down Expand Up @@ -34,7 +35,7 @@ fn exercise3() {

let values_number = values.len();

let additions: Vec<usize> = vec![0];
let additions = &vec![0];

println!("{:?}", values_number);

Expand All @@ -43,18 +44,18 @@ fn exercise3() {

// Sumar valores en additions
for element_index in additions {
let addition_aux = values[element_index];
let addition_aux = values[*element_index];
addition = addition_aux + addition;
}
}
}

// Exercise 4
// Make it compile
fn exercise4(value: u32) -> &'static str {
fn exercise4(value: u32) -> String {
let str_value = value.to_string(); // Convert u32 to String
let str_ref: &str = &str_value; // Obtain a reference to the String
str_ref // Return the reference to the String
str_ref.to_string() // Return the reference to the String
}

// Exercise 5
Expand All @@ -68,9 +69,9 @@ fn exercise5() {
let res = match my_map.get(&key) {
Some(child) => child,
None => {
let value = "3.0".to_string();
my_map.insert(key, value);
&value // HERE IT FAILS
let value = "3.0";
my_map.insert(key, value.to_string());
value // HERE IT FAILS
}
};

Expand All @@ -83,14 +84,15 @@ fn exercise5() {
use std::io;

fn exercise6() {
let mut prev_key: &str = "";
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];
prev_key = data[0].to_string();
}
}
}
Expand All @@ -100,8 +102,8 @@ fn exercise6() {
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();
let chars = &[b'x', b'y', b'z'];
let s: &str = std::str::from_utf8(chars).unwrap();
v.push(&s);
}
println!("{:?}", v);
Expand All @@ -113,10 +115,10 @@ fn exercise8() {
let mut accounting = vec!["Alice", "Ben"];

loop {
let mut add_input = String::from("");
let mut add_input = "";

io::stdin()
.read_line(&mut add_input)
.read_line(&mut add_input.to_string())
.expect("Failed to read line");

let add_vec: Vec<&str> = add_input.trim()[..].split_whitespace().collect();
Expand Down