Confusion on syntax for supertraits

⚓ Rust    📅 2025-08-25    👤 surdeus    👁️ 6      

surdeus

Hello :waving_hand:, I have a basic data structure that behaves much like a vector (with a few key differences that prohibits using actual vectors, hashsets or treesets). Since it's such a basic building block of the computation I would like to easily swap different implementations. So a trait is the obvious answer.
Something like:

pub trait BasicStruct: Clone + Eq + Ord +  Default + IntoIterator
{
	fn is_subset(&self, other: &Self) -> bool;
	fn is_disjoint(&self, other: &Self) -> bool;
	fn union(&self, other: &Self) -> Self;
	fn extend(&self, other: &Self) -> Self;
	fn intersection(&self, other: &Self) -> Self;
	fn subtraction(&self, other: &Self) -> Self;
	fn len(&self) -> usize;
	fn is_empty(&self) -> bool;
	...
}

But I'm not satisfied with just IntoIterator, I need iter() to be implemented.
Since there is no trait for that, I need to qualify IntoIterator for &Self somehow.
What is the syntax? Can i actually do it or should i just require fn iter(...) -> ... in my trait?

6 posts - 3 participants

Read full topic

🏷️ Rust_feed