4 ms·
You need to type your Python code extremely fast to finish it 10 times faster than this Rust program: extern crate xpath_reader; extern crate regex;
by v_lisivka 9y ago
You need to type your Python code extremely fast to finish it 10 times faster than this Rust program:
extern crate xpath_reader;
extern crate regex;
use std::io::prelude::*;
use std::fs::File;
use xpath_reader::Reader;
use regex::Regex;
fn main() {
let mut contents = String::new();
File::open("test.xml").expect("Unable to open the file").read_to_string(&mut contents).expect("Unable to read the file");
//println!("File contents: {}", contents);
let reader = Reader::from_str(&contents, None).expect("Cannot parse XML file");
let files: Vec<String> = reader.read("//file").expect("Cannot find file entries in XML file");
//println!("Files: {:?}", files);
for file in files.iter() {
println!("File: {}", file);
let mut contents = String::new();
File::open(file).expect("Unable to open the file").read_to_string(&mut contents).expect("Unable to read the file");
//println!("File contents: {}", contents);
let re = Regex::new(r"foo bar").expect("Cannot compile regex");
let replaced = re.replace_all(&contents, "baz").to_string();
//println!("File contents: {}", replaced);
File::create(file).expect("Unable to overwrite the file").write(replaced.as_bytes()).expect("Unable to write to file");
}
}
- IshKebab 9y agoIt was rather more complicated than that. I could go into details but that's not really the point. It's not the typing that is slow - it's the constant "why can't I do it like this?" stackoverflow trips. For example - how do you open a file in read-write mode? Using File::open()? Nope! Ok that one is just poor API design - most of the time was really in restructuring to make the borrow checker happy, and fiddling with adding and removing '&' and 'ref' to get the right types.
- cortesoft 9y agoThis is a really silly counterpoint. I am not even sure what point you are trying to make. How can pasting some program show how long it would take someone to reason out how to write it?