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










share|improve this question




















  • 1




    You are "handling" the error returned by foo(), but not the error returned by map_err(...).
    – Sebastian Redl
    Nov 19 at 14:58















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










share|improve this question




















  • 1




    You are "handling" the error returned by foo(), but not the error returned by map_err(...).
    – Sebastian Redl
    Nov 19 at 14:58













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










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 by foo(), but not the error returned by map_err(...).
    – Sebastian Redl
    Nov 19 at 14:58














  • 1




    You are "handling" the error returned by foo(), but not the error returned by map_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












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),
_ => ()
}





share|improve this answer



















  • 4




    Or foo().unwrap_or_else(|err| println!("{:?}", err))
    – Jmb
    Nov 19 at 7:49











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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

























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),
_ => ()
}





share|improve this answer



















  • 4




    Or foo().unwrap_or_else(|err| println!("{:?}", err))
    – Jmb
    Nov 19 at 7:49















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),
_ => ()
}





share|improve this answer



















  • 4




    Or foo().unwrap_or_else(|err| println!("{:?}", err))
    – Jmb
    Nov 19 at 7:49













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),
_ => ()
}





share|improve this answer














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),
_ => ()
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 19 at 5:19

























answered Nov 19 at 5:14









Benjamin Lindley

85.3k3134223




85.3k3134223








  • 4




    Or foo().unwrap_or_else(|err| println!("{:?}", err))
    – Jmb
    Nov 19 at 7:49














  • 4




    Or foo().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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Costa Masnaga

Fotorealismo

Sidney Franklin