You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 ownershipfnstring_uppercase(mutdata:&String){let data_uc = data.to_uppercase();println!("{}", data_uc);}
or without another variable
// Should take ownershipfnstring_uppercase(mutdata:&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.
Hinted solution
rustlings/exercises/06_move_semantics/move_semantics6.rs
Lines 23 to 28 in 7105310
Based on hints and code comments, the expected solution here is to make
string_uppercase()
take ownership ofdata
. When done, the compiler prompts to remove&
from line 25 to match the type of variabledata
.Actual error
The actual error shown (before making any changes) is temporary value dropped while borrowed.
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,
or without another variable
I understand that in
main()
,data
is not used after callingstring_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
The text was updated successfully, but these errors were encountered: