I want to write a struct which has a take
function and also implements the Iterator
trait. How can I call the take
associated function instead of the trait function? The compiler calls the take of the Iterator
trait. How it works? The Option
type also has a take
as associated function and implements iterator, but it call associated function when I write option.take()
pub struct Wrapper<T> {
inner: Vec<T>,
}
impl<T> Wrapper<T> {
pub fn take(&mut self) -> Option<T> {
self.inner.pop()
}
}
impl<T> Iterator for Wrapper<T> {
type Item = T;
fn next(&mut self) -> Option<Self::Item> {
Self::take(self)
}
}
fn main() {
let mut wrapper = Wrapper {
inner: vec![],
};
let _ = wrapper.take();
}
Errors:
Compiling playground v0.0.1 (/playground)
error[E0061]: this function takes 1 argument but 0 arguments were supplied
--> src/main.rs:24:21
|
24 | let _ = wrapper.take();
| ^^^^- supplied 0 arguments
| |
| expected 1 argument
|
note: associated function defined here
For more information about this error, try `rustc --explain E0061`.
error: could not compile `playground` due to previous error