diff --git a/crates/roc_std/src/roc_dict.rs b/crates/roc_std/src/roc_dict.rs index 04d51cd266..916e01af8a 100644 --- a/crates/roc_std/src/roc_dict.rs +++ b/crates/roc_std/src/roc_dict.rs @@ -58,6 +58,39 @@ impl RocDict { } } +impl<'a, K, V> IntoIterator for &'a RocDict { + type Item = (&'a K, &'a V); + type IntoIter = IntoIter<'a, K, V>; + + fn into_iter(self) -> Self::IntoIter { + IntoIter { + index: 0, + items: &self, + } + } +} + +pub struct IntoIter<'a, K, V> { + index: usize, + items: &'a RocDict, +} + +impl<'a, K, V> Iterator for IntoIter<'a, K, V> { + type Item = (&'a K, &'a V); + + fn next(&mut self) -> Option { + let item = self + .items + .0 + .get(self.index) + .map(|item| (&item.key, &item.value)); + + self.index += 1; + + item + } +} + impl Debug for RocDict { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_str("RocDict ")?;