O'Reilly logo

Mastering Rust by Vesa Kaihlavirta

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Copy trait

When a type implements the Copy trait (like all the primitive number types do), every new binding causes a new copy of the value instead of a move. This is why the num2 binding in the example before was fine: it caused a new copy to be created and num1 was left intact and still usable.

High level programming languages do similar things, but hide what actually happens behind the curtain of the implementation. For instance, check out these assignment operations followed by mutations in Python:

Since strings in Python are immutable, the assignment operation must copy the string and the mutation makes yet a third copy. On the other ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required