Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Error handling 无法将文件内容读取为字符串-结果在名为'read\u to\u string'的作用域中未实现任何方法`_Error Handling_Rust - Fatal编程技术网

Error handling 无法将文件内容读取为字符串-结果在名为'read\u to\u string'的作用域中未实现任何方法`

Error handling 无法将文件内容读取为字符串-结果在名为'read\u to\u string'的作用域中未实现任何方法`,error-handling,rust,Error Handling,Rust,我按照代码从以下位置打开文件: let file = match file { Ok(file) => file, Err(error) => { pani

我按照代码从以下位置打开文件:

let file = match file {                                                
    Ok(file) => file,                                                  
    Err(error) => {                                                    
        panic!("Problem opening the file: {:?}", error)                
    },                                                                 
};                                                                     
使用std:{env,fs::File,path::path};
fn main(){
让args:Vec=env::args().collect();
let pattern=&args[1];
如果让某些(a)=env::args().nth(2){
让路径=路径::新建(&a);
让mut file=file::open(&path);
让mut s=String::new();
读取到字符串(&mut s);
println!(“{:?}”,s);
}否则{
//做点什么
}
}
然而,我得到了这样一个信息:

let file = match file {                                                
    Ok(file) => file,                                                  
    Err(error) => {                                                    
        panic!("Problem opening the file: {:?}", error)                
    },                                                                 
};                                                                     
error[E0599]:在当前作用域中找不到类型为'std::result::result'的名为'read_to_string'的方法
-->src/main.rs:11:14
|
11 | file.read_to_string(&mut s);
|在'std::result::result'中找不到^`

我做错了什么?

让我们看看您的错误消息:

let file = match file {                                                
    Ok(file) => file,                                                  
    Err(error) => {                                                    
        panic!("Problem opening the file: {:?}", error)                
    },                                                                 
};                                                                     
error[E0599]:在当前作用域中找不到类型为'std::result::result'的名为'read_to_string'的方法
-->src/main.rs:11:14
|
11 | file.read_to_string(&mut s);
|在'std::result::result'中找不到^`
错误消息与tin上显示的内容基本相同-类型不具有方法
读取字符串
。事实上,这是一个错误

let file = match file {                                                
    Ok(file) => file,                                                  
    Err(error) => {                                                    
        panic!("Problem opening the file: {:?}", error)                
    },                                                                 
};                                                                     
您有一个
结果
,因为
文件::打开(&path)
可以失败。故障用
结果
类型表示。
结果可以是成功案例,也可以是失败案例

let file = match file {                                                
    Ok(file) => file,                                                  
    Err(error) => {                                                    
        panic!("Problem opening the file: {:?}", error)                
    },                                                                 
};                                                                     
你需要设法处理这个失败的案例。最简单的方法是使用
expect

let mut file = File::open(&path).expect("Unable to open");
let file = match file {                                                
    Ok(file) => file,                                                  
    Err(error) => {                                                    
        panic!("Problem opening the file: {:?}", error)                
    },                                                                 
};                                                                     
您还需要将
Read
纳入范围,才能访问
Read\u to\u string

use std::io::Read;
let file = match file {                                                
    Ok(file) => file,                                                  
    Err(error) => {                                                    
        panic!("Problem opening the file: {:?}", error)                
    },                                                                 
};                                                                     
我强烈建议大家通读并使用这些例子。本章将具有高度相关性。我认为这些文件是一流的

您也可以使用“?”

let file = match file {                                                
    Ok(file) => file,                                                  
    Err(error) => {                                                    
        panic!("Problem opening the file: {:?}", error)                
    },                                                                 
};                                                                     
但是在这种情况下,您的方法应该返回
Result

let file = match file {                                                
    Ok(file) => file,                                                  
    Err(error) => {                                                    
        panic!("Problem opening the file: {:?}", error)                
    },                                                                 
};                                                                     

let file = match file {                                                
    Ok(file) => file,                                                  
    Err(error) => {                                                    
        panic!("Problem opening the file: {:?}", error)                
    },                                                                 
};                                                                     

有关更多信息,请参阅此

:在这里,它确实说明该文件有一个read_to_string方法。为什么我不能像第一个示例代码那样在这里访问它?@user3918985
文件
实现了
读取
,它提供了
读取到字符串
。我不明白你说的“这里”是什么意思。你必须
使用
这个特性(如我所示)才能将这些方法纳入范围。谢谢你的全面解释。我也刚开始学习rust,但你还必须导入“Read”这个事实对我来说很糟糕。File对象已经实现了trait Read,因此异常是否应该告诉我必须导入Read,而不是简单地说我缺少一个方法?@dtc:
help:以下trait已实现,但不在范围内;也许可以为它添加一个“use”:use std::io::Read。问题是,
Result
没有答案中所述的方法。值得补充的是,
操作符是一种简洁的方式,可以很早地展开或返回错误:让mut file=file::open(&path)?看见
let file = match file {                                                
    Ok(file) => file,                                                  
    Err(error) => {                                                    
        panic!("Problem opening the file: {:?}", error)                
    },                                                                 
};