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

move_semantics6 intended solution does not address the error #1963

Open
ramenhost opened this issue May 3, 2024 · 0 comments
Open

move_semantics6 intended solution does not address the error #1963

ramenhost opened this issue May 3, 2024 · 0 comments

Comments

@ramenhost
Copy link

ramenhost commented May 3, 2024

Hinted solution

// Should take ownership
fn string_uppercase(mut data: &String) {
data = &data.to_uppercase();
println!("{}", data);
}

Based on hints and code comments, the expected solution here is to make string_uppercase() take ownership of data. When done, the compiler prompts to remove & from line 25 to match the type of variable data.

Actual error

The actual error shown (before making any changes) is temporary value dropped while borrowed.

error[E0716]: temporary value dropped while borrowed
  --> exercises/06_move_semantics/move_semantics6.rs:23:13
   |
22 | fn string_uppercase(mut data: &String) {
   |                               - let's call the lifetime of this reference `'1`
23 |     data = &data.to_uppercase();
   |     --------^^^^^^^^^^^^^^^^^^^- temporary value is freed at the end of this statement
   |     |       |
   |     |       creates a temporary value which is freed while still in use
   |     assignment requires that borrow lasts for `'1`

Addressing the compiler error

The real problem here is not that string_uppercase takes reference. It i s because in line 25, a reference to temporary value is created. Since the temporary value goes out of scope after line 25, the reference out-lives the value being referred to.
If we were to follow this thread, we would end up with a solution that does not necessarily take ownership. Some examples below,

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

    println!("{}", data_uc);
}

or without another variable

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

    println!("{}", data);
}

I understand that in main(), data is not used after calling string_uppercase() and hence it is valid solution to take ownership. However, my concern is that it does not address the root cause, rather fixes error as a side effect. As someone using rustlings to learn rust, this confused me.

Environment

@ramenhost ➜ /workspaces/rustlings (main) $ rustc --version
rustc 1.77.2 (25ef9e3d8 2024-04-09)
@ramenhost ➜ /workspaces/rustlings (main) $ rustlings --version
rustlings 5.6.1
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