My previous question tells me that rust cannot take reference to itself in a struct.
using self in new constructor
So my question would become: how to design a struct when I need to reference to itself?
We might take this struct as an example:
struct SplitByChars<'a> {
seperator: &'a Seperator,
string: String,
chars_iter: std::str::Chars<'a>,
}
impl<'a> SplitByChars<'a> {
fn new<S>(seperator: &'a Seperator, string: S) -> SplitByChars where S: Into<String> {
SplitByChars {
seperator: seperator,
string: string.into(),
chars_iter: self.string.chars(), // error here: I cannot use self (of course, static method)
}
}
}
I used chars_iter
to provide an interface of iterable string splitting.
(This is just an example, so I'd like to know about a more general idea on designing the struct, not specially in this splitting case. Moreover, no std's split.)
Thx in advance!