Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
baiyazi233 committed Apr 8, 2024
1 parent 7ef2d0d commit 76d54ad
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 20 deletions.
4 changes: 2 additions & 2 deletions exercises/move_semantics/move_semantics1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
// Execute `rustlings hint move_semantics1` or use the `hint` watch subcommand
// for a hint.

// I AM NOT DONE


fn main() {
let vec0 = Vec::new();

let vec1 = fill_vec(vec0);
let mut vec1 = fill_vec(vec0);

println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);

Expand Down
4 changes: 2 additions & 2 deletions exercises/move_semantics/move_semantics2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
// Execute `rustlings hint move_semantics2` or use the `hint` watch subcommand
// for a hint.

// I AM NOT DONE


fn main() {
let vec0 = Vec::new();

let mut vec1 = fill_vec(vec0);
let mut vec1 = fill_vec(vec0.clone());

println!("{} has length {}, with contents: `{:?}`", "vec0", vec0.len(), vec0);

Expand Down
3 changes: 2 additions & 1 deletion exercises/move_semantics/move_semantics3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// Execute `rustlings hint move_semantics3` or use the `hint` watch subcommand
// for a hint.

// I AM NOT DONE


fn main() {
let vec0 = Vec::new();
Expand All @@ -21,6 +21,7 @@ fn main() {
}

fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
let mut vec = vec;
vec.push(22);
vec.push(44);
vec.push(66);
Expand Down
4 changes: 2 additions & 2 deletions exercises/move_semantics/move_semantics4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// Execute `rustlings hint move_semantics4` or use the `hint` watch subcommand
// for a hint.

// I AM NOT DONE


fn main() {
let vec0 = Vec::new();
Expand All @@ -22,7 +22,7 @@ fn main() {
}

// `fill_vec()` no longer takes `vec: Vec<i32>` as argument
fn fill_vec() -> Vec<i32> {
fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
let mut vec = vec;

vec.push(22);
Expand Down
4 changes: 2 additions & 2 deletions exercises/move_semantics/move_semantics5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
// Execute `rustlings hint move_semantics5` or use the `hint` watch subcommand
// for a hint.

// I AM NOT DONE


fn main() {
let mut x = 100;
let y = &mut x;
let z = &mut x;
*y += 100;
let z = &mut x;
*z += 1000;
assert_eq!(x, 1200);
}
12 changes: 6 additions & 6 deletions exercises/move_semantics/move_semantics6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@
// Execute `rustlings hint move_semantics6` or use the `hint` watch subcommand
// for a hint.

// I AM NOT DONE


fn main() {
let data = "Rust is great!".to_string();

get_char(data);
get_char(&data);

string_uppercase(&data);
string_uppercase(data);
}

// Should not take ownership
fn get_char(data: String) -> char {
fn get_char(data: &String) -> char {
data.chars().last().unwrap()
}

// Should take ownership
fn string_uppercase(mut data: &String) {
data = &data.to_uppercase();
fn string_uppercase(mut data: String) {
data = data.to_uppercase();

println!("{}", data);
}
4 changes: 2 additions & 2 deletions exercises/vecs/vecs1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
//
// 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>) {
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];// TODO: declare your vector here with the macro for vectors

(a, v)
}
Expand Down
6 changes: 3 additions & 3 deletions exercises/vecs/vecs2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
//
// 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> {
for element in v.iter_mut() {
// TODO: Fill this up so that each element in the Vec `v` is
// multiplied by 2.
???
*element *= 2;
}

// At this point, `v` should be equal to [4, 8, 12, 16, 20].
Expand All @@ -24,7 +24,7 @@ fn vec_map(v: &Vec<i32>) -> Vec<i32> {
v.iter().map(|element| {
// TODO: Do the same thing as above - but instead of mutating the
// Vec, you can just return the new number!
???
element*2
}).collect()
}

Expand Down

0 comments on commit 76d54ad

Please sign in to comment.