Loops 从文件中读取行,迭代每行和该行中的每个字符
我需要读取一个文件,获取每一行,遍历每一行,并检查该行是否包含来自“aeiuo”的任何字符,以及是否至少包含两个字符“äüö” 这段代码是习惯用法吗?如何检查Loops 从文件中读取行,迭代每行和该行中的每个字符,loops,character,line,rust,contains,Loops,Character,Line,Rust,Contains,我需要读取一个文件,获取每一行,遍历每一行,并检查该行是否包含来自“aeiuo”的任何字符,以及是否至少包含两个字符“äüö” 这段代码是习惯用法吗?如何检查字符串中的几个字符 到目前为止,我在谷歌和代码窃取方面的尝试: use std::error::Error; use std::fs::File; use std::io::BufReader; use std::io::prelude::*; use std::path::Path; fn main() { // Create
字符串中的几个字符
到目前为止,我在谷歌和代码窃取方面的尝试:
use std::error::Error;
use std::fs::File;
use std::io::BufReader;
use std::io::prelude::*;
use std::path::Path;
fn main() {
// Create a path to the desired file
let path = Path::new("foo.txt");
let display = path.display();
// Open the path in read-only mode, returns `io::Result<File>`
let file = match File::open(&path) {
// The `description` method of `io::Error` returns a string that describes the error
Err(why) => panic!("couldn't open {}: {}", display, Error::to_string(&why)),
Ok(file) => file,
};
// Collect all lines into a vector
let reader = BufReader::new(file);
let lines: Vec<_> = reader.lines().collect();
for l in lines {
if (l.unwrap().contains("a")) {
println!("here is a");
}
}
}
使用std::error::error;
使用std::fs::File;
使用std::io::BufReader;
使用std::io::prelude::*;
使用std::path::path;
fn main(){
//创建所需文件的路径
让path=path::new(“foo.txt”);
let display=path.display();
//以只读模式打开路径,返回'io::Result'`
让文件=匹配文件::打开(&path){
//`io::Error`的`description`方法返回一个描述错误的字符串
Err(why)=>panic!(“无法打开{}:{}”,显示,错误::to_字符串(&why)),
确定(文件)=>文件,
};
//将所有行收集到一个向量中
让reader=BufReader::new(文件);
让行:Vec=reader.lines().collect();
对于l行{
如果(l.unwrap()包含(“a”)){
println!(“这是一个”);
}
}
}
1)“这段代码是习惯用法吗?”
总的来说,不错。有一点您可能需要改进:您不需要将行收集到向量中进行迭代。这是不需要的,因为它会触发不需要的内存分配。直接读取行()
迭代器就可以了。(如果你来自C++,你可以忘记把东西收集到中间向量:想想函数,想想迭代器!)
然后您可以一直使用迭代器,并按如下方式编写主要测试:
let reader = BufReader::new(file);
let lines = reader.lines();
let bad_line = lines.map(|l| l.unwrap()).filter(|line| !valid(line)).next();
match bad_line {
Some(line_n) => println!("Line {} doesn't pass the test", line_n),
None => println!("All lines are good!"),
}
// Alternate way if you don't need the line number. More readable
//let all_good = lines.map(|l| l.unwrap()).all(valid);
(屏幕上的完整代码。)此功能:
使用std::error::error;
使用std::fs::File;
使用std::io::BufReader;
使用std::io::prelude::*;
使用std::path::path;
fn是_元音(x:&char)->bool{
“aaeeiiouu”.chars().any(| y | y==*x)
}
fn是_umlaut(x:&char)->bool{
chars().any(| y | y==*x)
}
fn有效(行:&str)->bool{
line.chars().all(| c |!is|元音(&c))和line.chars().filter(is|umlaut.fuse().nth(1).is|some()
}
fn main(){
//创建所需文件的路径
让path=path::new(“c.txt”);
let display=path.display();
//以只读模式打开路径,返回'io::Result'`
让文件=匹配文件::打开(&path){
Err(why)=>panic!(“无法打开{}:{}”,显示,错误::说明(&why)),
确定(文件)=>文件,
};
让reader=BufReader::new(文件);
对于读卡器中的行。行(){
火线{
Ok(行)=>{
如果有效(&行){
println!(“{}”,行)
}
}
Err(e)=>println!(“错误:{}”,e),
}
}
}
这听起来像两个问题:“我的代码好/惯用吗”和“如何在字符串中查找多个字符”。这些应该分开;后者对于堆栈溢出来说是一个很好的问题,但是第一个问题应该提交给-确保您遵守他们的提交规则!这里需要工作代码。我同意@Shepmaster,但我想回答这个问题:谢谢,我将用以下内容改进我的代码:)。谢谢,我是一个爱好java程序员,而整个.iter().filter().map()对我来说都是新东西,还没有任何函数编程。如果没有IDE中的自动完成,所有不同的选项都很难记住。在这种情况下,请随意阅读和重新阅读,这很好!我遇到了一个奇怪的错误——我获取了您的代码,经过编译,到目前为止没有问题,但是使用了我的测试文件“foo.txt”,它在前3行中包含“üü”、“ää”、“aö”,我在控制台中得到这样的信息:“未通过测试”。“线{}”的部分被切断。将行更改为“未通过测试{}”会给我“未通过测试”?让我恼火的是,“ää”没有出现?这个解决方案是不正确的:它不能处理“aɁuɁo”:这些是使用多个代码点的分解形式。在进行比较时,您应该将输入标准化,并且应该使用graphemes
迭代器而不是chars
迭代器(为此,您需要clarets.io中的unicode分段
包,除非您想使用夜间和不稳定的功能)。@Vega:不幸的是,这不是一项容易的任务,因为Unicode很复杂。我建议您阅读有关代码点、字形、组合字符序列的内容,您将了解您的字符串可能相当复杂。至于显示字符串的问题,您确定您的终端已正确配置为显示UTF-8吗?
let reader = BufReader::new(file);
let lines = reader.lines();
// lines is a instance of some type which implements Iterator<Item=&str>
for l in lines {
...
}
fn is_aeiou(x: &char) -> bool {
"aeiou".chars().any(|y| y == *x)
}
fn is_weird_auo(x: &char) -> bool {
"äüö".chars().any(|y| y == *x)
}
fn valid(line: &str) -> bool {
line.chars().any(|c| is_aeiou(&c)) &&
line.chars().filter(is_weird_auo).fuse().nth(1).is_some()
}
let reader = BufReader::new(file);
let lines = reader.lines();
let bad_line = lines.map(|l| l.unwrap()).filter(|line| !valid(line)).next();
match bad_line {
Some(line_n) => println!("Line {} doesn't pass the test", line_n),
None => println!("All lines are good!"),
}
// Alternate way if you don't need the line number. More readable
//let all_good = lines.map(|l| l.unwrap()).all(valid);
use std::error::Error;
use std::fs::File;
use std::io::BufReader;
use std::io::prelude::*;
use std::path::Path;
fn is_vowel(x: &char) -> bool {
"aAeEiIoOuU".chars().any(|y| y == *x)
}
fn is_umlaut(x: &char) -> bool {
"äÄüÜöÖ".chars().any(|y| y == *x)
}
fn valid(line: &str) -> bool {
line.chars().all(|c| !is_vowel(&c)) && line.chars().filter(is_umlaut).fuse().nth(1).is_some()
}
fn main() {
// Create a path to the desired file
let path = Path::new("c.txt");
let display = path.display();
// Open the path in read-only mode, returns `io::Result<File>`
let file = match File::open(&path) {
Err(why) => panic!("couldn't open {}: {}", display, Error::description(&why)),
Ok(file) => file,
};
let reader = BufReader::new(file);
for line in reader.lines() {
match line {
Ok(line) => {
if valid(&line) {
println!("{}", line)
}
}
Err(e) => println!("ERROR: {}", e),
}
}
}