Reputation: 675
I am trying to access a variable inside a for
loop. I can't implement Copy
on the struct because it contains a String
. How would I use the variable across iterations?
I get error E0382 when compiling. When I looked at the Rust documentation for the error, they mentioned using reference counting to solve the problem. Is this the only solution in my case?
#[derive(Clone)]
struct InputParser {
args: Vec<String>,
current: String,
consumed_quote: bool,
}
impl InputParser {
pub fn parse(input: String) -> Vec<String> {
let parser = InputParser {
args: Vec::new(),
current: String::new(),
consumed_quote: false,
};
for c in input.chars() {
match c {
'"' => parser.consume_quote(),
' ' => parser.consume_space(),
_ => parser.consume_char(c),
}
}
parser.end();
return parser.args;
}
pub fn consume_space(mut self) {
if !self.consumed_quote {
self.push_current();
}
}
pub fn consume_quote(mut self) {
self.consumed_quote = self.consumed_quote;
if self.consumed_quote {
self.push_current();
}
}
pub fn consume_char(mut self, c: char) {
self.current.push(c);
}
pub fn end(mut self) {
self.push_current();
}
pub fn push_current(mut self) {
if self.current.len() > 0 {
self.args.push(self.current);
self.current = String::new();
}
}
}
I want to access parser
across iterations of the for
loop.
Upvotes: 0
Views: 128
Reputation: 432189
[How do I] move [a] non-copyable struct across iterations
You don't, at least not trivially. Once you've moved the struct to a function, it's gone. The only way to get it back is for the function to give it back to you.
Instead, you most likely want to modify an existing struct inside the loop. You need to use a mutable reference for this:
use std::mem;
#[derive(Clone)]
struct InputParser {
args: Vec<String>,
current: String,
consumed_quote: bool,
}
impl InputParser {
fn consume_space(&mut self) {
if !self.consumed_quote {
self.push_current();
}
}
fn consume_quote(&mut self) {
self.consumed_quote = self.consumed_quote;
if self.consumed_quote {
self.push_current();
}
}
fn consume_char(&mut self, c: char) {
self.current.push(c);
}
fn end(&mut self) {
self.push_current();
}
fn push_current(&mut self) {
if self.current.len() > 0 {
let arg = mem::replace(&mut self.current, String::new());
self.args.push(arg);
}
}
}
fn parse(input: String) -> Vec<String> {
let mut parser = InputParser {
args: Vec::new(),
current: String::new(),
consumed_quote: false,
};
for c in input.chars() {
match c {
'"' => parser.consume_quote(),
' ' => parser.consume_space(),
_ => parser.consume_char(c),
}
}
parser.end();
parser.args
}
fn main() {}
Note that the previous way of taking the current argument would result in error[E0507]: cannot move out of borrowed content
, so I switched to mem::replace
. This prevents self.current
from ever becoming an undefined value (which it was previously).
If you really want to pass everything by value, you need to return by value as well.
#[derive(Clone)]
struct InputParser {
args: Vec<String>,
current: String,
consumed_quote: bool,
}
impl InputParser {
fn consume_space(mut self) -> Self {
if !self.consumed_quote {
return self.push_current();
}
self
}
fn consume_quote(mut self) -> Self {
self.consumed_quote = self.consumed_quote;
if self.consumed_quote {
return self.push_current();
}
self
}
fn consume_char(mut self, c: char) -> Self {
self.current.push(c);
self
}
fn end(mut self) -> Self {
self.push_current()
}
fn push_current(mut self) -> Self {
if self.current.len() > 0 {
self.args.push(self.current);
self.current = String::new();
}
self
}
}
fn parse(input: String) -> Vec<String> {
let mut parser = InputParser {
args: Vec::new(),
current: String::new(),
consumed_quote: false,
};
for c in input.chars() {
parser = match c {
'"' => parser.consume_quote(),
' ' => parser.consume_space(),
_ => parser.consume_char(c),
}
}
parser = parser.end();
parser.args
}
fn main() {}
I believe this makes the API objectively worse in this case. However, you will see this style somewhat frequently with a builder. In that case, the methods tend to be chained together, so you never see a reassignment to the variable.
Upvotes: 4