|
|
|
|
|
by TheDong
998 days ago
|
|
In go, the stdlib impl is actually: func Clone[S ~[]E, E any](s S) S {
if s == nil {
return nil
}
return append(S([]E{}), s...)
}
For comparison, `vec.clone()` in the rust stdlib is: pub trait Clone: Sized {
fn clone(&self) -> Self;
}
impl<T: Clone> Clone for Vec<T> {
fn clone(&self) -> Self {
<[T]>::to_vec(&**self)
}
}
I think the rust one is much easier to read. The go one has an if statement, which means the go one has higher cyclomatic complexity, and is thus harder to understand and reason about.The rust one does have "&**self", which looks a little strange perhaps, but overall seems simpler than the go one. |
|