admin管理员组文章数量:1024945
In this code, what is the difference between adding the ?
operator after the call to read_to_string()
and not adding it? Why do both work = what happens if this call fails?
fn chaining() -> Result<String, std::io::Error> {
let mut username = String::new();
File::open("fake.txt")?.read_to_string(&mut username)?; // <- removing ? here also works
Ok(username)
}
In this code, what is the difference between adding the ?
operator after the call to read_to_string()
and not adding it? Why do both work = what happens if this call fails?
fn chaining() -> Result<String, std::io::Error> {
let mut username = String::new();
File::open("fake.txt")?.read_to_string(&mut username)?; // <- removing ? here also works
Ok(username)
}
Share
Improve this question
asked Nov 18, 2024 at 18:59
evilmandarineevilmandarine
4,5934 gold badges22 silver badges49 bronze badges
1 Answer
Reset to default 4If you remove the ?
operator, errors when reading the file will be silently ignored (although the compiler will warn you). If you keep it, errors will return from the function with Err
. So you probably want to keep it.
As for what happens to the String
passed if you ignore the error, the documentation of read_to_string()
refers to the documentation of read_to_end()
, which says the following:
If any other read error is encountered then this function immediately returns. Any bytes which have already been read will be appended to
buf
.
In this code, what is the difference between adding the ?
operator after the call to read_to_string()
and not adding it? Why do both work = what happens if this call fails?
fn chaining() -> Result<String, std::io::Error> {
let mut username = String::new();
File::open("fake.txt")?.read_to_string(&mut username)?; // <- removing ? here also works
Ok(username)
}
In this code, what is the difference between adding the ?
operator after the call to read_to_string()
and not adding it? Why do both work = what happens if this call fails?
fn chaining() -> Result<String, std::io::Error> {
let mut username = String::new();
File::open("fake.txt")?.read_to_string(&mut username)?; // <- removing ? here also works
Ok(username)
}
Share
Improve this question
asked Nov 18, 2024 at 18:59
evilmandarineevilmandarine
4,5934 gold badges22 silver badges49 bronze badges
1 Answer
Reset to default 4If you remove the ?
operator, errors when reading the file will be silently ignored (although the compiler will warn you). If you keep it, errors will return from the function with Err
. So you probably want to keep it.
As for what happens to the String
passed if you ignore the error, the documentation of read_to_string()
refers to the documentation of read_to_end()
, which says the following:
If any other read error is encountered then this function immediately returns. Any bytes which have already been read will be appended to
buf
.
本文标签: rustWhat is the difference between adding theoperator and not adding it in this callStack Overflow
版权声明:本文标题:rust - What is the difference between adding the `?` operator and not adding it in this call? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745600031a2158401.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论