Friends
I am processing text one line at a time using regular expressions. The structure of the line determines how it is processed.
A concrete example:
A line: "Sample rate: 48000" I want to store the sample rate and move on to the next line
A line: "xrun detected" I want to increment a counter and move on to the next line.
Coming from Perl I would say:
if($line =~ /Sample rate: (\d+)/){
$sample_rate = $1;
next;
I want to do that in Rust, but I cannot figure out how. I have achieved my goal, but without any elegance
I have the regular expression:
sample_rate_re: Regex::new(r"Samplerate: (\d+)").unwrap()
if sample_rate_re.captures(line).is_some() {
let caps = sample_rate_re.captures(line).unwrap();
let sample_rate_text: &str = caps.get(1).map_or("0", |m| -> &str m.as_str());
sample_rate = sample_rate_text.parse().unwrap();
continue;
}
But I am using the regular expression twice.
I would like to say:
if caps = sample_rate_re.captures(line).unwrap() {
let sample_rate_text: &str = caps.get(1).map_or("0", |m| -> &str m.as_str());
but that is not valid Rust.
Is there a way to do this?