Ejercicios 32/94
This commit is contained in:
parent
f79b84cd8e
commit
99745f1acf
|
@ -1,12 +1,10 @@
|
||||||
// move_semantics1.rs
|
// move_semantics1.rs
|
||||||
// Execute `rustlings hint move_semantics1` or use the `hint` watch subcommand for a hint.
|
// Execute `rustlings hint move_semantics1` or use the `hint` watch subcommand for a hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let vec0 = Vec::new();
|
let vec0 = Vec::new();
|
||||||
|
|
||||||
let vec1 = fill_vec(vec0);
|
let mut vec1 = fill_vec(vec0);
|
||||||
|
|
||||||
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
|
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
|
||||||
|
|
||||||
|
|
|
@ -2,12 +2,10 @@
|
||||||
// Make me compile without changing line 13 or moving line 10!
|
// Make me compile without changing line 13 or moving line 10!
|
||||||
// Execute `rustlings hint move_semantics2` or use the `hint` watch subcommand for a hint.
|
// Execute `rustlings hint move_semantics2` or use the `hint` watch subcommand for a hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let vec0 = Vec::new();
|
let vec0 = Vec::new();
|
||||||
|
|
||||||
let mut vec1 = fill_vec(vec0);
|
let mut vec1 = fill_vec(vec0.clone());
|
||||||
|
|
||||||
// Do not change the following line!
|
// Do not change the following line!
|
||||||
println!("{} has length {} content `{:?}`", "vec0", vec0.len(), vec0);
|
println!("{} has length {} content `{:?}`", "vec0", vec0.len(), vec0);
|
||||||
|
|
|
@ -3,8 +3,6 @@
|
||||||
// (no lines with multiple semicolons necessary!)
|
// (no lines with multiple semicolons necessary!)
|
||||||
// Execute `rustlings hint move_semantics3` or use the `hint` watch subcommand for a hint.
|
// Execute `rustlings hint move_semantics3` or use the `hint` watch subcommand for a hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let vec0 = Vec::new();
|
let vec0 = Vec::new();
|
||||||
|
|
||||||
|
@ -17,7 +15,7 @@ fn main() {
|
||||||
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
|
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
|
fn fill_vec(mut vec: Vec<i32>) -> Vec<i32> {
|
||||||
vec.push(22);
|
vec.push(22);
|
||||||
vec.push(44);
|
vec.push(44);
|
||||||
vec.push(66);
|
vec.push(66);
|
||||||
|
|
|
@ -4,12 +4,8 @@
|
||||||
// function.
|
// function.
|
||||||
// Execute `rustlings hint move_semantics4` or use the `hint` watch subcommand for a hint.
|
// Execute `rustlings hint move_semantics4` or use the `hint` watch subcommand for a hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let vec0 = Vec::new();
|
let mut vec1 = fill_vec();
|
||||||
|
|
||||||
let mut vec1 = fill_vec(vec0);
|
|
||||||
|
|
||||||
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
|
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
|
||||||
|
|
||||||
|
@ -20,7 +16,7 @@ fn main() {
|
||||||
|
|
||||||
// `fill_vec()` no longer takes `vec: Vec<i32>` as argument
|
// `fill_vec()` no longer takes `vec: Vec<i32>` as argument
|
||||||
fn fill_vec() -> Vec<i32> {
|
fn fill_vec() -> Vec<i32> {
|
||||||
let mut vec = vec;
|
let mut vec = Vec::new();
|
||||||
|
|
||||||
vec.push(22);
|
vec.push(22);
|
||||||
vec.push(44);
|
vec.push(44);
|
||||||
|
|
|
@ -3,13 +3,11 @@
|
||||||
// adding, changing or removing any of them.
|
// adding, changing or removing any of them.
|
||||||
// Execute `rustlings hint move_semantics5` or use the `hint` watch subcommand for a hint.
|
// Execute `rustlings hint move_semantics5` or use the `hint` watch subcommand for a hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut x = 100;
|
let mut x = 100;
|
||||||
let y = &mut x;
|
let y = &mut x;
|
||||||
let z = &mut x;
|
|
||||||
*y += 100;
|
*y += 100;
|
||||||
|
let z = &mut x;
|
||||||
*z += 1000;
|
*z += 1000;
|
||||||
assert_eq!(x, 1200);
|
assert_eq!(x, 1200);
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,24 +2,22 @@
|
||||||
// Execute `rustlings hint move_semantics6` or use the `hint` watch subcommand for a hint.
|
// Execute `rustlings hint move_semantics6` or use the `hint` watch subcommand for a hint.
|
||||||
// You can't change anything except adding or removing references.
|
// You can't change anything except adding or removing references.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let data = "Rust is great!".to_string();
|
let data = "Rust is great!".to_string();
|
||||||
|
|
||||||
get_char(data);
|
get_char(&data);
|
||||||
|
|
||||||
string_uppercase(&data);
|
string_uppercase(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Should not take ownership
|
// Should not take ownership
|
||||||
fn get_char(data: String) -> char {
|
fn get_char(data: &String) -> char {
|
||||||
data.chars().last().unwrap()
|
data.chars().last().unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Should take ownership
|
// Should take ownership
|
||||||
fn string_uppercase(mut data: &String) {
|
fn string_uppercase(mut data: String) {
|
||||||
data = &data.to_uppercase();
|
data = data.to_uppercase();
|
||||||
|
|
||||||
println!("{}", data);
|
println!("{}", data);
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,13 +2,13 @@
|
||||||
// Address all the TODOs to make the tests pass!
|
// Address all the TODOs to make the tests pass!
|
||||||
// Execute `rustlings hint structs1` or use the `hint` watch subcommand for a hint.
|
// Execute `rustlings hint structs1` or use the `hint` watch subcommand for a hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
struct ColorClassicStruct {
|
struct ColorClassicStruct {
|
||||||
// TODO: Something goes here
|
red: u32,
|
||||||
|
green: u32,
|
||||||
|
blue: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ColorTupleStruct(/* TODO: Something goes here */);
|
struct ColorTupleStruct(u32, u32, u32);
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct UnitLikeStruct;
|
struct UnitLikeStruct;
|
||||||
|
@ -19,8 +19,11 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn classic_c_structs() {
|
fn classic_c_structs() {
|
||||||
// TODO: Instantiate a classic c struct!
|
let green = ColorClassicStruct {
|
||||||
// let green =
|
red: 0,
|
||||||
|
green: 255,
|
||||||
|
blue: 0,
|
||||||
|
};
|
||||||
|
|
||||||
assert_eq!(green.red, 0);
|
assert_eq!(green.red, 0);
|
||||||
assert_eq!(green.green, 255);
|
assert_eq!(green.green, 255);
|
||||||
|
@ -29,8 +32,7 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn tuple_structs() {
|
fn tuple_structs() {
|
||||||
// TODO: Instantiate a tuple struct!
|
let green = ColorTupleStruct(0, 255, 0);
|
||||||
// let green =
|
|
||||||
|
|
||||||
assert_eq!(green.0, 0);
|
assert_eq!(green.0, 0);
|
||||||
assert_eq!(green.1, 255);
|
assert_eq!(green.1, 255);
|
||||||
|
@ -39,8 +41,7 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn unit_structs() {
|
fn unit_structs() {
|
||||||
// TODO: Instantiate a unit-like struct!
|
let unit_like_struct = UnitLikeStruct;
|
||||||
// let unit_like_struct =
|
|
||||||
let message = format!("{:?}s are fun!", unit_like_struct);
|
let message = format!("{:?}s are fun!", unit_like_struct);
|
||||||
|
|
||||||
assert_eq!(message, "UnitLikeStructs are fun!");
|
assert_eq!(message, "UnitLikeStructs are fun!");
|
||||||
|
|
|
@ -2,8 +2,6 @@
|
||||||
// Address all the TODOs to make the tests pass!
|
// Address all the TODOs to make the tests pass!
|
||||||
// Execute `rustlings hint structs2` or use the `hint` watch subcommand for a hint.
|
// Execute `rustlings hint structs2` or use the `hint` watch subcommand for a hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct Order {
|
struct Order {
|
||||||
name: String,
|
name: String,
|
||||||
|
@ -34,8 +32,11 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn your_order() {
|
fn your_order() {
|
||||||
let order_template = create_order_template();
|
let order_template = create_order_template();
|
||||||
// TODO: Create your own order using the update syntax and template above!
|
let your_order = Order {
|
||||||
// let your_order =
|
name: String::from("Hacker in Rust"),
|
||||||
|
count: 1,
|
||||||
|
..order_template
|
||||||
|
};
|
||||||
assert_eq!(your_order.name, "Hacker in Rust");
|
assert_eq!(your_order.name, "Hacker in Rust");
|
||||||
assert_eq!(your_order.year, order_template.year);
|
assert_eq!(your_order.year, order_template.year);
|
||||||
assert_eq!(your_order.made_by_phone, order_template.made_by_phone);
|
assert_eq!(your_order.made_by_phone, order_template.made_by_phone);
|
||||||
|
|
|
@ -4,11 +4,10 @@
|
||||||
// Make me compile and pass the test!
|
// Make me compile and pass the test!
|
||||||
// Execute `rustlings hint vecs1` or use the `hint` watch subcommand for a hint.
|
// Execute `rustlings hint vecs1` or use the `hint` watch subcommand for a hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
fn array_and_vec() -> ([i32; 4], Vec<i32>) {
|
fn array_and_vec() -> ([i32; 4], Vec<i32>) {
|
||||||
let a = [10, 20, 30, 40]; // a plain array
|
let a = [10, 20, 30, 40]; // a plain array
|
||||||
let v = // TODO: declare your vector here with the macro for vectors
|
let v = a.to_vec();
|
||||||
|
// let v = vec![10, 20, 30, 40];
|
||||||
|
|
||||||
(a, v)
|
(a, v)
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,13 +6,9 @@
|
||||||
//
|
//
|
||||||
// Execute `rustlings hint vecs2` or use the `hint` watch subcommand for a hint.
|
// Execute `rustlings hint vecs2` or use the `hint` watch subcommand for a hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
fn vec_loop(mut v: Vec<i32>) -> Vec<i32> {
|
fn vec_loop(mut v: Vec<i32>) -> Vec<i32> {
|
||||||
for i in v.iter_mut() {
|
for i in v.iter_mut() {
|
||||||
// TODO: Fill this up so that each element in the Vec `v` is
|
*i *= 2
|
||||||
// multiplied by 2.
|
|
||||||
???
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// At this point, `v` should be equal to [4, 8, 12, 16, 20].
|
// At this point, `v` should be equal to [4, 8, 12, 16, 20].
|
||||||
|
@ -21,9 +17,7 @@ fn vec_loop(mut v: Vec<i32>) -> Vec<i32> {
|
||||||
|
|
||||||
fn vec_map(v: &Vec<i32>) -> Vec<i32> {
|
fn vec_map(v: &Vec<i32>) -> Vec<i32> {
|
||||||
v.iter().map(|num| {
|
v.iter().map(|num| {
|
||||||
// TODO: Do the same thing as above - but instead of mutating the
|
num * 2
|
||||||
// Vec, you can just return the new number!
|
|
||||||
???
|
|
||||||
}).collect()
|
}).collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue