Clone returns a copy of the slice. The elements are copied using assignment, so this is a shallow clone. The difference with slices.Clone is that the result will not have additional unused capacity.
(s S)
| 4 | // The elements are copied using assignment, so this is a shallow clone. |
| 5 | // The difference with slices.Clone is that the result will not have additional unused capacity. |
| 6 | func Clone[S ~[]E, E any](s S) S { |
| 7 | // Preserve nilness in case it matters. |
| 8 | if s == nil { |
| 9 | return nil |
| 10 | } |
| 11 | return append(make(S, 0, len(s)), s...) |
| 12 | } |