diff --git a/crates/nu-protocol/src/value/record.rs b/crates/nu-protocol/src/value/record.rs index 9297a7496752a..c3f4590b04497 100644 --- a/crates/nu-protocol/src/value/record.rs +++ b/crates/nu-protocol/src/value/record.rs @@ -203,6 +203,30 @@ impl Record { self.cols.truncate(retained); } + /// Truncate record to the first `len` elements. + /// + /// `len > self.len()` will be ignored + /// ```rust + /// use nu_protocol::{record, Value}; + /// + /// let mut rec = record!( + /// "a" => Value::test_nothing(), + /// "b" => Value::test_int(42), + /// "c" => Value::test_nothing(), + /// "d" => Value::test_int(42), + /// ); + /// rec.truncate(42); // this is fine + /// assert_eq!(rec.columns().map(String::as_str).collect::(), "abcd"); + /// rec.truncate(2); // truncate + /// assert_eq!(rec.columns().map(String::as_str).collect::(), "ab"); + /// rec.truncate(0); // clear the record + /// assert_eq!(rec.len(), 0); + /// ``` + pub fn truncate(&mut self, len: usize) { + self.cols.truncate(len); + self.vals.truncate(len); + } + pub fn columns(&self) -> Columns { Columns { iter: self.cols.iter(),