If you run cargo clippy on this you will get the following warning:
warning: unused `std::result::Result` that must be used
--> src/main.rs:12:5
|
12 | func("hello world");
| ^^^^^^^^^^^^^^^^^^^^
|
this is telling you that although func returns a Result which could be an error, you are not doing anything with it.
You need to handle the error - the simplest way to do this is to just change main to return an error:
fn main() -> Result<(), Box<dyn Error>> {
func("hello world")?; // If this fails, the `?` makes main return it
Ok(())
}
Now is func fails, main will fail too.
To see this in action we need an error type - which is a bit tedious to implement (for example see here for details). Instead, I'll just convert to using anyhow for errors.
use anyhow::{anyhow, Result};
fn func<T>(val: T) -> Result<()>
where
T: std::fmt::Debug,
{
println!("{:?}", val);
Err(anyhow!("BANG"))
}
fn main() -> Result<()> {
func("hello world")?;
Ok(())
}
Now func returns an error, and running the binary outputs:
"hello world"
Error: BANG
If you don't want your application to exit on error, or don't want it to exit in this way, then you need to handle the result from func yourself
use anyhow::{anyhow, Result};
fn func<T>(val: T) -> Result<()>
where
T: std::fmt::Debug,
{
println!("{:?}", val);
Err(anyhow!("BANG"))
}
fn main() {
match func("hello world") {
Err(e) => println!("an error: {:?}", e), //<= error handling
Ok(_) => println!("func was OK"),
}
}
which will output:
"hello world"
an error: BANG