42
Filling In Random Bits
A real collection in the std-collections style implements a long tail of traits: Default, Clone, Extend, FromIterator, Debug, PartialEq/Eq, PartialOrd/Ord, Hash, plus the simple is_empty, len, clear methods. Most of them are one-liners on top of iter().
- Trivial methods first.
lenandis_emptyare direct reads.clearis*self = Self::new()afterDrop-equivalent work — easiest is to drop in place viamem::take-of-self isn't possible withoutDefault, so just looppop_front.impl<T> LinkedList<T> { pub fn len(&self) -> usize { self.len } pub fn is_empty(&self) -> bool { self.len == 0 } pub fn clear(&mut self) { while self.pop_front().is_some() {} } } impl<T> Default for LinkedList<T> { fn default() -> Self { LinkedList::new() } } -
ExtendandFromIterator: take an iterator, push each element onto the back.FromIteratorbuilds a new list and forwards toExtend. Both are short becausepush_backdoes the real work.impl<T> Extend<T> for LinkedList<T> { fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) { for item in iter { self.push_back(item); } } } impl<T> FromIterator<T> for LinkedList<T> { fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self { let mut list = LinkedList::new(); list.extend(iter); list } } - Comparison and hashing all delegate to iterating both lists in lockstep.
PartialEqwalks element-wise;PartialOrd/Orduse lexicographic order;Hashmixes in the length followed by every element.Debuguses the formatter'sdebug_listhelper.use std::fmt; use std::hash::{Hash, Hasher}; impl<T: fmt::Debug> fmt::Debug for LinkedList<T> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_list().entries(self).finish() } } impl<T: PartialEq> PartialEq for LinkedList<T> { fn eq(&self, other: &Self) -> bool { self.len == other.len && self.iter().eq(other.iter()) } } impl<T: Eq> Eq for LinkedList<T> {} impl<T: PartialOrd> PartialOrd for LinkedList<T> { fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> { self.iter().partial_cmp(other.iter()) } } impl<T: Ord> Ord for LinkedList<T> { fn cmp(&self, other: &Self) -> std::cmp::Ordering { self.iter().cmp(other.iter()) } } impl<T: Hash> Hash for LinkedList<T> { fn hash<H: Hasher>(&self, state: &mut H) { self.len.hash(state); for item in self { item.hash(state); } } }