struct S {
foo: i32,
// ... other fields
}
fn f(s: S) {
// transform foo
let new_foo = biz_logic(s.foo);
// from now on, the code should read `new_foo` whenever it needs `s.foo`,
// how do I "drop" the `s.foo` to prevent misuse?
// obviously `drop(s.foo)` won't work since it is `Copy`
// is there any idiom to do this?
drop(s.foo);
// OK
let _ = ... new_foo ...;
// Not OK
let _ = ... s.foo ...;
}
Is there less known rust feature that let's you achieve the same result as drop() on a Copy?
One option is to use variable shadowing. However, you can't shadow
s.foo, so unless you're willing to replace its value withnew_fooand keep usings.foo, you can binds.footo a local variable,foo, then shadow it with the result ofbiz_logic: