String 为什么BufReader::lines中的行不匹配?

String 为什么BufReader::lines中的行不匹配?,string,file-io,rust,string-parsing,String,File Io,Rust,String Parsing,我正在尝试解析文本文件,并使用BufReader查找字符串是否等于当前行: 让mut chapter=String::fromChapter 1\n; //打开文件 让file=file::opendocument.txt?; 让reader=BufReader::newfile; 对于reader.line中的line{ 如果章节eq和line.unwrap{ println!找到章节!; } } 但是,if语句永远不会返回true。如何正确地将reader.lines中的行转换为可以找到匹配

我正在尝试解析文本文件,并使用BufReader查找字符串是否等于当前行:

让mut chapter=String::fromChapter 1\n; //打开文件 让file=file::opendocument.txt?; 让reader=BufReader::newfile; 对于reader.line中的line{ 如果章节eq和line.unwrap{ println!找到章节!; } } 但是,if语句永远不会返回true。如何正确地将reader.lines中的行转换为可以找到匹配项的方式?

来自以下文档:

返回的每个字符串末尾都不会有换行字节0xA字节或CRLF 0xD、0xA字节

从搜索字符串中删除\n:

让mut chapter=String::fromChapter 1; 没有理由在这里分配字符串。其他一些变化:

使用std::{ fs::文件, io:{self,BufRead,BufReader}, }; fn示例->io::结果{ 让第章=第1章; 让file=file::opendocument.txt?; 让reader=BufReader::newfile; 对于reader.line中的line{ 如果章节==行。展开{ println!找到章节!; } } 好啊 } 另见:

根据以下文件:

返回的每个字符串末尾都不会有换行字节0xA字节或CRLF 0xD、0xA字节

从搜索字符串中删除\n:

让mut chapter=String::fromChapter 1; 没有理由在这里分配字符串。其他一些变化:

使用std::{ fs::文件, io:{self,BufRead,BufReader}, }; fn示例->io::结果{ 让第章=第1章; 让file=file::opendocument.txt?; 让reader=BufReader::newfile; 对于reader.line中的line{ 如果章节==行。展开{ println!找到章节!; } } 好啊 } 另见:


非常感谢你的帮助!我真的是个新手,所以我非常感谢你。非常感谢你的帮助!我真的是个新手,所以我非常感激。