Skip to content

Commit 06f34fc

Browse files
authored
Merge pull request #1693 from dtolnay/get
Add Punctuated::get and get_mut
2 parents f0dfdbd + a443857 commit 06f34fc

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/punctuated.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,29 @@ impl<T, P> Punctuated<T, P> {
9292
self.iter_mut().next_back()
9393
}
9494

95+
/// Borrows the element at the given index.
96+
pub fn get(&mut self, index: usize) -> Option<&T> {
97+
if let Some((value, _punct)) = self.inner.get(index) {
98+
Some(value)
99+
} else if index == self.inner.len() {
100+
self.last.as_deref()
101+
} else {
102+
None
103+
}
104+
}
105+
106+
/// Mutably borrows the element at the given index.
107+
pub fn get_mut(&mut self, index: usize) -> Option<&mut T> {
108+
let inner_len = self.inner.len();
109+
if let Some((value, _punct)) = self.inner.get_mut(index) {
110+
Some(value)
111+
} else if index == inner_len {
112+
self.last.as_deref_mut()
113+
} else {
114+
None
115+
}
116+
}
117+
95118
/// Returns an iterator over borrowed syntax tree nodes of type `&T`.
96119
pub fn iter(&self) -> Iter<T> {
97120
Iter {

0 commit comments

Comments
 (0)