Why am I getting “unused Result which must be used … Result may be an Err variant, which should be...
up vote
0
down vote
favorite
fn main() {
foo().map_err(|err| println!("{:?}", err));
}
fn foo() -> Result<(), std::io::Error> {
Ok(())
}
results:
warning: unused `std::result::Result` that must be used
--> src/main.rs:2:5
|
2 | foo().map_err(|err| println!("{:?}", err));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: #[warn(unused_must_use)] on by default
= note: this `Result` may be an `Err` variant, which should be handled
Finished dev [unoptimized + debuginfo] target(s) in 0.58s
Running `target/debug/playground`
playground link
rust
add a comment |
up vote
0
down vote
favorite
fn main() {
foo().map_err(|err| println!("{:?}", err));
}
fn foo() -> Result<(), std::io::Error> {
Ok(())
}
results:
warning: unused `std::result::Result` that must be used
--> src/main.rs:2:5
|
2 | foo().map_err(|err| println!("{:?}", err));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: #[warn(unused_must_use)] on by default
= note: this `Result` may be an `Err` variant, which should be handled
Finished dev [unoptimized + debuginfo] target(s) in 0.58s
Running `target/debug/playground`
playground link
rust
1
You are "handling" the error returned byfoo()
, but not the error returned bymap_err(...)
.
– Sebastian Redl
Nov 19 at 14:58
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
fn main() {
foo().map_err(|err| println!("{:?}", err));
}
fn foo() -> Result<(), std::io::Error> {
Ok(())
}
results:
warning: unused `std::result::Result` that must be used
--> src/main.rs:2:5
|
2 | foo().map_err(|err| println!("{:?}", err));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: #[warn(unused_must_use)] on by default
= note: this `Result` may be an `Err` variant, which should be handled
Finished dev [unoptimized + debuginfo] target(s) in 0.58s
Running `target/debug/playground`
playground link
rust
fn main() {
foo().map_err(|err| println!("{:?}", err));
}
fn foo() -> Result<(), std::io::Error> {
Ok(())
}
results:
warning: unused `std::result::Result` that must be used
--> src/main.rs:2:5
|
2 | foo().map_err(|err| println!("{:?}", err));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: #[warn(unused_must_use)] on by default
= note: this `Result` may be an `Err` variant, which should be handled
Finished dev [unoptimized + debuginfo] target(s) in 0.58s
Running `target/debug/playground`
playground link
rust
rust
edited Nov 19 at 14:26
Shepmaster
145k11274408
145k11274408
asked Nov 19 at 4:25
marathon
2,61294591
2,61294591
1
You are "handling" the error returned byfoo()
, but not the error returned bymap_err(...)
.
– Sebastian Redl
Nov 19 at 14:58
add a comment |
1
You are "handling" the error returned byfoo()
, but not the error returned bymap_err(...)
.
– Sebastian Redl
Nov 19 at 14:58
1
1
You are "handling" the error returned by
foo()
, but not the error returned by map_err(...)
.– Sebastian Redl
Nov 19 at 14:58
You are "handling" the error returned by
foo()
, but not the error returned by map_err(...)
.– Sebastian Redl
Nov 19 at 14:58
add a comment |
1 Answer
1
active
oldest
votes
up vote
6
down vote
accepted
You're not handling the result, you're mapping the result from one type to another.
foo().map_err(|err| println!("{:?}", err));
What that line does is call foo()
, which returns Result<(), std::io::Error>
. Then map_err
uses the type returned by your closure (in this case, ()
), and modifies the error type and returns Result<(), ()>
. This is the result that you are not handling. Since you seem to want to just ignore this result, the simplest thing to do would probably be to call ok()
.
foo().map_err(|err| println!("{:?}", err)).ok();
ok()
converts Result<T,E>
to Option<T>
, converting errors to None
, which you won't get a warning for ignoring.
Alternatively:
match foo() {
Err(e) => println!("{:?}", e),
_ => ()
}
4
Orfoo().unwrap_or_else(|err| println!("{:?}", err))
– Jmb
Nov 19 at 7:49
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
6
down vote
accepted
You're not handling the result, you're mapping the result from one type to another.
foo().map_err(|err| println!("{:?}", err));
What that line does is call foo()
, which returns Result<(), std::io::Error>
. Then map_err
uses the type returned by your closure (in this case, ()
), and modifies the error type and returns Result<(), ()>
. This is the result that you are not handling. Since you seem to want to just ignore this result, the simplest thing to do would probably be to call ok()
.
foo().map_err(|err| println!("{:?}", err)).ok();
ok()
converts Result<T,E>
to Option<T>
, converting errors to None
, which you won't get a warning for ignoring.
Alternatively:
match foo() {
Err(e) => println!("{:?}", e),
_ => ()
}
4
Orfoo().unwrap_or_else(|err| println!("{:?}", err))
– Jmb
Nov 19 at 7:49
add a comment |
up vote
6
down vote
accepted
You're not handling the result, you're mapping the result from one type to another.
foo().map_err(|err| println!("{:?}", err));
What that line does is call foo()
, which returns Result<(), std::io::Error>
. Then map_err
uses the type returned by your closure (in this case, ()
), and modifies the error type and returns Result<(), ()>
. This is the result that you are not handling. Since you seem to want to just ignore this result, the simplest thing to do would probably be to call ok()
.
foo().map_err(|err| println!("{:?}", err)).ok();
ok()
converts Result<T,E>
to Option<T>
, converting errors to None
, which you won't get a warning for ignoring.
Alternatively:
match foo() {
Err(e) => println!("{:?}", e),
_ => ()
}
4
Orfoo().unwrap_or_else(|err| println!("{:?}", err))
– Jmb
Nov 19 at 7:49
add a comment |
up vote
6
down vote
accepted
up vote
6
down vote
accepted
You're not handling the result, you're mapping the result from one type to another.
foo().map_err(|err| println!("{:?}", err));
What that line does is call foo()
, which returns Result<(), std::io::Error>
. Then map_err
uses the type returned by your closure (in this case, ()
), and modifies the error type and returns Result<(), ()>
. This is the result that you are not handling. Since you seem to want to just ignore this result, the simplest thing to do would probably be to call ok()
.
foo().map_err(|err| println!("{:?}", err)).ok();
ok()
converts Result<T,E>
to Option<T>
, converting errors to None
, which you won't get a warning for ignoring.
Alternatively:
match foo() {
Err(e) => println!("{:?}", e),
_ => ()
}
You're not handling the result, you're mapping the result from one type to another.
foo().map_err(|err| println!("{:?}", err));
What that line does is call foo()
, which returns Result<(), std::io::Error>
. Then map_err
uses the type returned by your closure (in this case, ()
), and modifies the error type and returns Result<(), ()>
. This is the result that you are not handling. Since you seem to want to just ignore this result, the simplest thing to do would probably be to call ok()
.
foo().map_err(|err| println!("{:?}", err)).ok();
ok()
converts Result<T,E>
to Option<T>
, converting errors to None
, which you won't get a warning for ignoring.
Alternatively:
match foo() {
Err(e) => println!("{:?}", e),
_ => ()
}
edited Nov 19 at 5:19
answered Nov 19 at 5:14
Benjamin Lindley
85.3k3134223
85.3k3134223
4
Orfoo().unwrap_or_else(|err| println!("{:?}", err))
– Jmb
Nov 19 at 7:49
add a comment |
4
Orfoo().unwrap_or_else(|err| println!("{:?}", err))
– Jmb
Nov 19 at 7:49
4
4
Or
foo().unwrap_or_else(|err| println!("{:?}", err))
– Jmb
Nov 19 at 7:49
Or
foo().unwrap_or_else(|err| println!("{:?}", err))
– Jmb
Nov 19 at 7:49
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53368303%2fwhy-am-i-getting-unused-result-which-must-be-used-result-may-be-an-err-vari%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
You are "handling" the error returned by
foo()
, but not the error returned bymap_err(...)
.– Sebastian Redl
Nov 19 at 14:58