This question already has an answer here:
Below I have a struct SplitByChars
.
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: I cannot use self here!
}
}
}
I am trying to implement a new
static function for it. In particular, I would like to return a SplitByChars
instance whose chars_iter
field is initialized using the previously initialized string
field of the same instance. To do that, I am currently trying to access the same field using self.string
, but I am getting an error.
How can I do it?
I think you're making things overly complicated. Splitting a string at specific chars can be done with:
Output:
There are several variations of the split function to suit various needs.
You can't. Unless I misunderstand your question, this is the same problem as this one. Structs generally cannot have references into themselves.