I'm using the regex crate to find some text with this regex:
lazy_static! {
static ref FIND_STEPS_RE: Regex =
Regex::new(r"my regex").unwrap();
}
I want to find all possible captures and iterate over them:
FIND_STEPS_RE.captures_iter(script_slice)
Each captured element consists of 2 values: an operation and a number. For example, the output could be:
[("+", "10"), ("-", "20"), ("*", "2")]
I want to iterate over it, parse the numbers and apply the operation.
I tried:
let e = FIND_STEPS_RE.captures_iter(script_slice)
.fold(0, |sum, value| apply_decoding_step)?;
where apply_decoding_step
is:
fn apply_decoding_step(sum: i32, capture: regex::Captures<>) -> Result<i32> {
let number = parse_number(&capture[2])?;
match &capture[1] {
"+" => Ok(s + number),
"-" => Ok(s - number),
"*" => Ok(s * number),
"/" => Ok(s / number),
_ => bail!("Unknown step operator"),
}
}
But I got this error:
error[E0271]: type mismatch resolving `<fn(i32, regex::Captures<'_>) -> std::result::Result<i32, Error> {apply_decoding_step} as std::ops::FnOnce<(i32, regex::Captures<'_>)>>::Output == i32`
--> src/main.rs:122:10
|
122 | .fold(seed, apply_decoding_step);
| ^^^^ expected enum `std::result::Result`, found i32
|
= note: expected type `std::result::Result<i32, Error>`
found type `i32`
I assume this is because I'm trying to fold a Result
into a i32
, but since I need to parse the second capture value and also need that otherwise
case in my match
, how can I fix that?
let sum = sum?;
be shorter? – Parhe