部分的ムーブ
Within the destructuring of a single variable, both by-move
and by-reference
pattern bindings can be used at the same time. Doing this will result in a partial move of the variable, which means that parts of the variable will be moved while other parts stay. In such a case, the parent variable cannot be used afterwards as a whole, however the parts that are only referenced (and not moved) can still be used. Note that types that implement the Drop
trait cannot be partially moved from, because its drop
method would use it afterwards as a whole.
この例では、age
変数をヒープ上に保持し、部分的ムーブを説明しています。上記コードでref
を削除すると、person.age
の所有権がage
変数にムーブされるため、エラーになります。もしもPerson.age
がスタック上に保持されていたら、age
の定義がperson.age
をムーブすることなくデータをコピーするので、ref
は必須ではないのですが、実際にはヒープ上に保持されているためref
は必須です。