你好,我决定搜索文件中的特定文本,一种grep,有2种模式:敏感搜索:
pub fn search<'a>(_content: &'a str, _query: &str ) -> Vec<&'a str> {
_content.lines().filter(|line| line.contains(_query)).collect()
}
而且也不敏感:
pub fn search_without_lowcase<'a>(_contaitner: &'a str, _query: &str) -> Vec<&'a str>{
_contaitner.lines().filter(|line| line.to_lowercase().contains(&_query.to_lowercase())).collect()
}
选择使用哪一个是通过以下方式完成的:
let mut _lowacase = env::var("LOW").is_err();
所以这就是我遇到的问题:我无法实现(如果可以的话,多重选择,如果我选择一次,&env:LOW=0
它将不再等于1,并且为了使用不敏感搜索,我必须重新启动powershell..这是整个代码:
use std::{ env, fs, io};
pub struct Fileseach{
pub _filename: String,
pub _query: String,
pub _lowacase: bool
}
impl Fileseach {
pub fn new(mut args: env::Args) -> Result<Fileseach, &'static str>{
args.next();
let _filename = match args.next(){
Some(arg) => arg,
None => return Err("не удалось считать название файла"),
};
let _query = match args.next() {
Some(arg) => arg,
None => return Err("не удалось считать поисковый запрос"),
};
let mut _lowacase = env::var("LOW").is_err();
Ok(Fileseach{_filename, _query, _lowacase})
}
pub fn search<'a>(_content: &'a str, _query: &str ) -> Vec<&'a str> {
_content.lines().filter(|line| line.contains(_query)).collect()
}
pub fn search_without_lowcase<'a>(_contaitner: &'a str, _query: &str) -> Vec<&'a str>{
_contaitner.lines().filter(|line| line.to_lowercase().contains(&_query.to_lowercase())).collect()
}
pub fn run(_filesearch: Fileseach)-> Result<(), io::Error>{
let _content = fs::read_to_string(_filesearch._filename).expect("не удалось счиатть файл");
if _filesearch._lowacase {
for line in Fileseach::search_without_lowcase(&_content, &_filesearch._query){
println!("{}", line);
};
}
else{
for line in Fileseach::search(&_content, &_filesearch._query){
println!("{}", line);
};}
Ok(())
}
}