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

Improved move_semantics4 exercise proposal #1947

Open
Cai1Hsu opened this issue Apr 12, 2024 · 0 comments
Open

Improved move_semantics4 exercise proposal #1947

Cai1Hsu opened this issue Apr 12, 2024 · 0 comments

Comments

@Cai1Hsu
Copy link

Cai1Hsu commented Apr 12, 2024

Current:

#[test]
fn main() {
let vec0 = vec![22, 44, 66];
let vec1 = fill_vec(vec0);
assert_eq!(vec1, vec![22, 44, 66, 88]);
}
// `fill_vec()` no longer takes `vec: Vec<i32>` as argument - don't change this!
fn fill_vec() -> Vec<i32> {
// Instead, let's create and fill the Vec in here - how do you do that?
let mut vec = vec;
vec.push(88);
vec
}

Proposed Change:

#[test]
fn main() {
    // No changes should be made in the main function
    let vec1 = fill_vec();

    assert_eq!(vec1, vec![22, 44, 66, 88]);
}

// `fill_vec()` no longer takes `vec: Vec<i32>` as argument
fn fill_vec() -> ??? {
    // Instead, let's create and fill the Vec in here - how do you do that?
    let mut vec = ???

    vec.push(88);

    vec
}

The purpose of this exercise, as I understand it, is to help users grasp the concept of ownership and how values can be returned with ownership transferred back to the caller.

  1. Removed the declaration and usage of vec0 as it doesn't serve any purpose in this exercise. When I was solving this exercise, I thought I had to do something with it but at last, I found I had to remove it.
  2. Refactored the declaration of vec in fill_vec to guide learners towards what they should modify.
  3. Ask the user to determine the return type of fill_vec to help learners understand that they should return a value with ownership instead of a reference.
  4. Added a comment in main to instruct learners not to change the main function, allowing them to focus on modifying fill_vec.

This proposal might still have some imperfections so I opened this issue. Feel free to share your ideas.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant