diff options
| author | Zuhaitz Méndez Fernández de Aránguiz <zuhaitz@debian> | 2026-01-20 21:14:18 +0000 |
|---|---|---|
| committer | Zuhaitz Méndez Fernández de Aránguiz <zuhaitz@debian> | 2026-01-20 21:14:18 +0000 |
| commit | 7d3fb0f38333996f93ac9af190eefe5ca663cb70 (patch) | |
| tree | decd1e70221f50722e1fb0ef17764fa8bbd665da /README.md | |
| parent | c52637a16cfb1d94458453d3b4d11a80db191f2d (diff) | |
Fix 'Clone' among other things
Diffstat (limited to 'README.md')
| -rw-r--r-- | README.md | 23 |
1 files changed, 22 insertions, 1 deletions
@@ -457,7 +457,7 @@ fn peek(r: Resource*) { ... } // 'r' is borrowed (reference) If you *do* want two copies of a resource, make it explicit: ```zc -var b = a.clone(); // Deep copy: New allocation, safe. +var b = a.clone(); // Calls the 'clone' method from the Clone trait ``` **Opt-in Copy (Value Types)**: @@ -595,6 +595,27 @@ fn main() { } ``` +**Clone** + +Implement `Clone` to allow explicit duplication of resource-owning types. + +```zc +import "std/mem.zc" + +struct MyBox { val: int; } + +impl Clone for MyBox { + fn clone(self) -> MyBox { + return MyBox{val: self.val}; + } +} + +fn main() { + var b1 = MyBox{val: 42}; + var b2 = b1.clone(); // Explicit copy +} +``` + #### Composition Use `use` to embed other structs. You can either mix them in (flatten fields) or name them (nest fields). |
