pub struct Punctuated<T, P>(/* private fields */);Expand description
A punctuated sequence with optional trailing separator.
Use this for lists where trailing punctuation is allowed but not required,
such as array literals: [1, 2, 3] or [1, 2, 3,].
§Type Parameters
T: The value type (e.g., expression)P: The punctuation/separator type (e.g., comma token)
§Example
ⓘ
// Parse: item, item, item
let mut items = Punctuated::<Item, CommaToken>::new();
items.push_value(stream.parse()?);
while stream.peek::<CommaToken>() {
items.push_punct(stream.parse()?);
if !stream.peek::<Item>() { break; } // Allow trailing comma
items.push_value(stream.parse()?);
}Implementations§
Source§impl<T, P> Punctuated<T, P>
impl<T, P> Punctuated<T, P>
Sourcepub const POLICY: TrailingPolicy = TrailingPolicy::Optional
pub const POLICY: TrailingPolicy = TrailingPolicy::Optional
The trailing punctuation policy for this type.
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Creates a new sequence with pre-allocated capacity.
Sourcepub fn push_value(&mut self, value: T)
pub fn push_value(&mut self, value: T)
Pushes a value onto the sequence.
Sourcepub fn push_punct(&mut self, punct: P)
pub fn push_punct(&mut self, punct: P)
Attaches punctuation to the last value.
Sourcepub fn trailing_punct(&self) -> bool
pub fn trailing_punct(&self) -> bool
Returns whether the sequence has trailing punctuation.
The return value depends on the wrapper type’s policy:
Punctuated: delegates to inner storageTerminated: alwaystrueSeparated: alwaysfalse
Sourcepub fn into_inner(self) -> PunctuatedInner<T, P>
pub fn into_inner(self) -> PunctuatedInner<T, P>
Consumes the wrapper and returns the inner storage.
Methods from Deref<Target = PunctuatedInner<T, P>>§
pub fn push_value(&mut self, value: T)
pub fn push_punct(&mut self, punct: P)
pub fn trailing_punct(&self) -> bool
pub fn len(&self) -> usize
pub fn is_empty(&self) -> bool
pub fn capacity(&self) -> usize
pub fn reserve(&mut self, additional: usize)
pub fn shrink_to_fit(&mut self)
pub fn clear(&mut self)
pub fn iter(&self) -> impl Iterator<Item = &T>
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut T>
pub fn pairs(&self) -> impl Iterator<Item = (&T, Option<&P>)>
pub fn pairs_mut(&mut self) -> impl Iterator<Item = (&mut T, Option<&mut P>)>
pub fn first(&self) -> Option<&T>
pub fn last(&self) -> Option<&T>
Trait Implementations§
Source§impl<T, P> AsMut<PunctuatedInner<T, P>> for Punctuated<T, P>
impl<T, P> AsMut<PunctuatedInner<T, P>> for Punctuated<T, P>
Source§fn as_mut(&mut self) -> &mut PunctuatedInner<T, P>
fn as_mut(&mut self) -> &mut PunctuatedInner<T, P>
Converts this type into a mutable reference of the (usually inferred) input type.
Source§impl<T, P> AsRef<PunctuatedInner<T, P>> for Punctuated<T, P>
impl<T, P> AsRef<PunctuatedInner<T, P>> for Punctuated<T, P>
Source§fn as_ref(&self) -> &PunctuatedInner<T, P>
fn as_ref(&self) -> &PunctuatedInner<T, P>
Converts this type into a shared reference of the (usually inferred) input type.
Source§impl<T: Clone, P: Clone> Clone for Punctuated<T, P>
impl<T: Clone, P: Clone> Clone for Punctuated<T, P>
Source§fn clone(&self) -> Punctuated<T, P>
fn clone(&self) -> Punctuated<T, P>
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl<T, P> Default for Punctuated<T, P>
impl<T, P> Default for Punctuated<T, P>
Source§impl<T, P> Deref for Punctuated<T, P>
impl<T, P> Deref for Punctuated<T, P>
Source§impl<T, P> DerefMut for Punctuated<T, P>
impl<T, P> DerefMut for Punctuated<T, P>
Source§impl<T, P> FromIterator<T> for Punctuated<T, P>
impl<T, P> FromIterator<T> for Punctuated<T, P>
Source§fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self
Creates a value from an iterator. Read more
Source§impl<T, P> IntoIterator for Punctuated<T, P>
impl<T, P> IntoIterator for Punctuated<T, P>
Auto Trait Implementations§
impl<T, P> Freeze for Punctuated<T, P>
impl<T, P> RefUnwindSafe for Punctuated<T, P>where
T: RefUnwindSafe,
P: RefUnwindSafe,
impl<T, P> Send for Punctuated<T, P>
impl<T, P> Sync for Punctuated<T, P>
impl<T, P> Unpin for Punctuated<T, P>
impl<T, P> UnwindSafe for Punctuated<T, P>where
T: UnwindSafe,
P: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more