From fde48228ac9a0507a4f259e4815a78c99ecd10e4 Mon Sep 17 00:00:00 2001 From: fgfm999 <96854585+fgfm999@users.noreply.github.com> Date: Sat, 23 Sep 2023 12:04:03 +0800 Subject: [PATCH] Remove explicit copy statement for vector (#9796) * Remove explicit copy statement for vector * update variables.md * update code comments * state compiler optimize behavior --- .../docs/move/book/variables.md | 20 +++++++++++++------ developer-docs-site/docs/move/book/vector.md | 14 ++----------- 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/developer-docs-site/docs/move/book/variables.md b/developer-docs-site/docs/move/book/variables.md index 4791dbc1ef928..0a8ceded383f4 100644 --- a/developer-docs-site/docs/move/book/variables.md +++ b/developer-docs-site/docs/move/book/variables.md @@ -714,25 +714,33 @@ before it is assigned a value. As mentioned above, the Move compiler will infer a `copy` or `move` if one is not indicated. The algorithm for doing so is quite simple: -- Any scalar value with the `copy` [ability](./abilities.md) is given a `copy`. +- Any value with the `copy` [ability](./abilities.md) is given a `copy`. - Any reference (both mutable `&mut` and immutable `&`) is given a `copy`. - Except under special circumstances where it is made a `move` for predictable borrow checker errors. - Any other value is given a `move`. - - This means that even though other values might be have the `copy` [ability](./abilities.md), it - must be done _explicitly_ by the programmer. - - This is to prevent accidental copies of large data structures. +- If the compiler can prove that the source value with copy ability is not used after the + assignment, then a move may be used instead of a copy for performance, but this will be invisible + to the programmer (except in possible decreased time or gas cost). For example: ```move +struct Foo { + f: u64 +} + +struct Coin has copy { + value: u64 +} + let s = b"hello"; let foo = Foo { f: 0 }; let coin = Coin { value: 0 }; -let s2 = s; // move +let s2 = s; // copy let foo2 = foo; // move -let coin2 = coin; // move +let coin2 = coin; // copy let x = 0; let b = false; diff --git a/developer-docs-site/docs/move/book/vector.md b/developer-docs-site/docs/move/book/vector.md index 6b99820bb5af5..95d7474262cf9 100644 --- a/developer-docs-site/docs/move/book/vector.md +++ b/developer-docs-site/docs/move/book/vector.md @@ -158,21 +158,11 @@ fun destroy_droppable_vector(vec: vector) { ``` Similarly, vectors cannot be copied unless the element type has `copy`. In other words, a -`vector` has `copy` if and only if `T` has `copy`. However, even copyable vectors are never -implicitly copied: - -```move -let x = vector::singleton(10); -let y = copy x; // compiler error without the copy! -``` - -Copies of large vectors can be expensive, so the compiler requires explicit `copy`'s to make it -easier to see where they are happening. +`vector` has `copy` if and only if `T` has `copy`. For more details see the sections on [type abilities](./abilities.md) and [generics](./generics.md). ## Ownership As mentioned [above](#destroying-and-copying-vectors), `vector` values can be copied only if the -elements can be copied. In that case, the copy must be explicit via a -[`copy`](./variables.md#move-and-copy) or a [dereference `*`](./references.md#reading-and-writing-through-references). +elements can be copied. \ No newline at end of file