Punctuated

Struct Punctuated 

Source
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>

Source

pub const POLICY: TrailingPolicy = TrailingPolicy::Optional

The trailing punctuation policy for this type.

Source

pub fn new() -> Self

Creates a new empty sequence.

Source

pub fn with_capacity(capacity: usize) -> Self

Creates a new sequence with pre-allocated capacity.

Source

pub fn push_value(&mut self, value: T)

Pushes a value onto the sequence.

Source

pub fn push_punct(&mut self, punct: P)

Attaches punctuation to the last value.

Source

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 storage
  • Terminated: always true
  • Separated: always false
Source

pub fn into_inner(self) -> PunctuatedInner<T, P>

Consumes the wrapper and returns the inner storage.

Methods from Deref<Target = PunctuatedInner<T, P>>§

Source

pub fn push_value(&mut self, value: T)

Source

pub fn push_punct(&mut self, punct: P)

Source

pub fn trailing_punct(&self) -> bool

Source

pub fn len(&self) -> usize

Source

pub fn is_empty(&self) -> bool

Source

pub fn capacity(&self) -> usize

Source

pub fn reserve(&mut self, additional: usize)

Source

pub fn shrink_to_fit(&mut self)

Source

pub fn clear(&mut self)

Source

pub fn iter(&self) -> impl Iterator<Item = &T>

Source

pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut T>

Source

pub fn pairs(&self) -> impl Iterator<Item = (&T, Option<&P>)>

Source

pub fn pairs_mut(&mut self) -> impl Iterator<Item = (&mut T, Option<&mut P>)>

Source

pub fn first(&self) -> Option<&T>

Source

pub fn last(&self) -> Option<&T>

Trait Implementations§

Source§

impl<T, P> AsMut<PunctuatedInner<T, P>> for Punctuated<T, P>

Source§

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>

Source§

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>

Source§

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)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug, P: Debug> Debug for Punctuated<T, P>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T, P> Default for Punctuated<T, P>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T, P> Deref for Punctuated<T, P>

Source§

type Target = PunctuatedInner<T, P>

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<T, P> DerefMut for Punctuated<T, P>

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
Source§

impl<T, P> FromIterator<T> for Punctuated<T, P>

Source§

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>

Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = Map<IntoIter<(T, Option<P>)>, fn((T, Option<P>)) -> T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more

Auto Trait Implementations§

§

impl<T, P> Freeze for Punctuated<T, P>

§

impl<T, P> RefUnwindSafe for Punctuated<T, P>

§

impl<T, P> Send for Punctuated<T, P>
where T: Send, P: Send,

§

impl<T, P> Sync for Punctuated<T, P>
where T: Sync, P: Sync,

§

impl<T, P> Unpin for Punctuated<T, P>
where T: Unpin, P: Unpin,

§

impl<T, P> UnwindSafe for Punctuated<T, P>
where T: UnwindSafe, P: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.