PNG -> JPG

This commit is contained in:
contre95 2023-01-04 13:16:54 +01:00
parent 07d96924b2
commit 1df535ddeb
3 changed files with 20 additions and 27 deletions

View File

@ -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);
@ -16,11 +14,9 @@ fn main() {
} }
fn fill_vec(vec: Vec<i32>) -> Vec<i32> { fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
let mut vec = vec; let mut vec_x = vec;
vec_x.push(22);
vec.push(22); vec_x.push(44);
vec.push(44); vec_x.push(66);
vec.push(66); vec_x
vec
} }

View File

@ -4,12 +4,9 @@
// 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 = vec![10, 20, 30, 40];
(a, v) (a, v)
} }

View File

@ -6,25 +6,24 @@
// //
// 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(v: &mut 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 // TODO: Fill this up so that each element in the Vec `v` is
// multiplied by 2. // multiplied by 2.
??? *i = *i * 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].
v
} }
fn vec_map(v: &Vec<i32>) -> Vec<i32> { fn vec_map(v: &Vec<i32>) -> Vec<i32> {
v.iter().map(|num| { v.iter()
// TODO: Do the same thing as above - but instead of mutating the .map(|num| {
// Vec, you can just return the new number! // TODO: Do the same thing as above - but instead of mutating the
??? // Vec, you can just return the new number!
}).collect() num * 2
})
.collect()
} }
#[cfg(test)] #[cfg(test)]
@ -33,10 +32,11 @@ mod tests {
#[test] #[test]
fn test_vec_loop() { fn test_vec_loop() {
let v: Vec<i32> = (1..).filter(|x| x % 2 == 0).take(5).collect(); let a: Vec<i32> = (1..).filter(|x| x % 2 == 0).take(5).collect();
let ans = vec_loop(v.clone()); let mut v: Vec<i32> = (1..).filter(|x| x % 2 == 0).take(5).collect();
vec_loop(&mut v);
assert_eq!(ans, v.iter().map(|x| x * 2).collect::<Vec<i32>>()); assert_eq!(v, a.iter().map(|x| x * 2).collect::<Vec<i32>>());
} }
#[test] #[test]