I'm new to Rust and going through the official book. I'm working on a simple grep example and want to make an exit function which I can use in different places. Unfortunately using this function in a closure in unwrap_or_else causes a compile error. This not clear to me why, because when I use the contents of the function directly in the closure it works.
Here is my main.rs file:
use std::env;
use std::fs;
use std::process;
use std::error::Error;
use std::fmt::Display;
struct Config{
query: String,
filename: String,
}
impl Config {
fn new(input: &[String]) -> Result<Config, &'static str> {
if input.len() < 3 {
return Err("Not enough arguments provided.");
}
let query = input[1].clone();
let filename = input[2].clone();
Ok(Config { query, filename })
}
}
fn run(cfg: Config) -> Result<(), Box<dyn Error>> {
let contents = fs::read_to_string(&cfg.filename)?;
contents.find(&cfg.query).expect("Corrupted text file.");
Ok(())
}
fn exit<T: Display>(msg: &str, err: T) {
println!("{}: {}", msg, err);
process::exit(1);
}
fn main() {
let args: Vec<String> = env::args().collect();
println!("{:?}", args);
let cfg = Config::new(&args).unwrap_or_else(|err| {
exit("Problem parsing arguments", err);
});
if let Err(err) = run(cfg) {
exit("Application error", err);
}
}
And here is the compile error:
error[E0308]: mismatched types
--> src\main.rs:41:55
|
41 | let cfg = Config::new(&args).unwrap_or_else(|err| {
| _______________________________________________________^
42 | | exit("Problem parsing arguments", err);
43 | | });
| |_____^ expected struct `Config`, found `()`
When I change the Config::new(&args).unwrap_or_else closure to this, it works:
let cfg = Config::new(&args).unwrap_or_else(|err| {
println!("Problem parsing arguments: {}", err);
process::exit(1);
});
I got stuck on this too. You need to import the process library:
use std::process;
edit: On second look you did import it. For others who run into this problem then that was mine. I got the same error.
You need to specify, that your exit() function never returns, i.e. add -> !.
These functions are called "diverging functions".
fn exit<T: Display>(msg: &str, err: T) -> ! {
println!("{}: {}", msg, err);
process::exit(1);
}
However, you should be careful with using process::exit(). Because it will terminate the current process, and not invoke destructors.
To ensure destructors are handled, you should instead do something like this:
fn main() {
std::process::exit(match run() {
Ok(_) => 0,
Err(code) => code,
});
}
fn run() -> Result<(), i32> {
// Application logic here, i.e. what you'd otherwise have had in `main()`
Ok(())
}
The example is a minor adapted version of the one found at the documentation for process::exit().
To add to vallentin's answer here's the more idiomatic version which doesn't use process::exit:
use std::env;
use std::error::Error;
use std::fmt::Display;
use std::fs;
use std::process;
struct Config {
query: String,
filename: String,
}
impl Config {
fn new(input: &[String]) -> Result<Config, &'static str> {
if input.len() < 3 {
return Err("Not enough arguments provided.");
}
let query = input[1].clone();
let filename = input[2].clone();
Ok(Config { query, filename })
}
}
fn run(cfg: Config) -> Result<(), Box<dyn Error>> {
let contents = fs::read_to_string(&cfg.filename)?;
// convert Option to a Result so we can use `?`
contents.find(&cfg.query).ok_or("Corrupted text file.")?;
Ok(())
}
// you can return a Result from main and Rust will
// print the error to the user if there is one
fn main() -> Result<(), Box<dyn Error>> {
let args: Vec<String> = env::args().collect();
println!("{:?}", args);
// use `?` instead of `exit` function
let cfg = Config::new(&args)?;
run(cfg)?;
Ok(())
}
playground
Related
I am new to rust and I have wrote a function which returns a Result with Box dyn Error.
use std::error::Error;
fn func<T>(val: T) -> Result<(), Box<dyn Error>>
where
T: std::fmt::Debug,
{
println!("{:?}", val);
Ok(())
}
fn main() {
func("hello world");
}
Here I am not writing any error logic in the function func but it still works. Does the above code automatically catches all errors? similar to python's
try:
# Run the code
except:
# Catch all the errors
Is there any universal error catching way in rust?
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
I'm trying to implement a simple interpreter in Rust, for which I have created a Tokens struct, which takes source characters and produces either a Token or a ScanError inside a Result:
pub struct Tokens<'src> {
chars: Chars<'src>,
}
impl<'src> Iterator for Tokens<'src> {
type Item = Result<Token, ScanError>;
fn next(&mut self) -> Option<Result<Token, ScanError>> {
// ...
}
}
Since Result implements FromIterator, it is simple to collect the result to either the first ScanError or a vector of Tokens:
fn scan_tokens(source: &str) -> Result<Vec<Token>, ScanError> {
let iter = Tokens {
chars: source.chars(),
};
iter.collect()
}
In the case of multiple errors I really want to return every error:
fn scan_tokens(source: &str) -> Result<Vec<Token>, Vec<ScanError>> {
// what goes here?
}
It isn't possible as far as I know to implement my own version of FromIterator because neither that trait or Result are local to my crate. Can anyone suggest a clean way of doing this?
I have written an implementation using partition on the iterator, then unwrapping each Result, below, but it's not fun to read and doesn't feel like good use of iterators:
type T = Vec<Result<Token, ScanError>>;
fn scan_tokens(source: &str) -> Result<Vec<Token>, Vec<ScanError>> {
let iter = Tokens {
chars: source.chars(),
};
let (tokens_results, error_results): (T, T) = iter.partition(|result| result.is_ok());
let errors: Vec<ScanError> = error_results
.into_iter()
.map(|result| result.unwrap_err())
.collect();
if errors.len() > 0 {
return Err(errors);
}
Ok(tokens_results
.into_iter()
.map(|result| result.unwrap())
.collect())
}
unwrapping each Result
I would use itertools' partition_map to avoid the need to unwrap:
use itertools::{Either, Itertools}; // 0.8.0
fn iterator() -> impl Iterator<Item = Result<i32, bool>> {
vec![Ok(1), Err(false), Ok(2), Err(true), Ok(3)].into_iter()
}
fn example() -> Result<Vec<i32>, Vec<bool>> {
let (values, errors): (Vec<_>, Vec<_>) = iterator().partition_map(|v| match v {
Ok(v) => Either::Left(v),
Err(e) => Either::Right(e),
});
if errors.is_empty() {
Ok(values)
} else {
Err(errors)
}
}
See also:
What's the most idiomatic way of working with an Iterator of Results?
How do I stop iteration and return an error when Iterator::map returns a Result::Err?
How do I perform iterator computations over iterators of Results without collecting to a temporary vector?
You could also use the fact that Option and Result implement IntoIterator to avoid the exact unwrap, although this still processes one collection twice:
fn example2() -> Result<Vec<i32>, Vec<bool>> {
let (values, errors): (Vec<_>, Vec<_>) = iterator().partition(|result| result.is_ok());
if errors.is_empty() {
Ok(values.into_iter().flat_map(Result::ok).collect())
} else {
Err(errors.into_iter().flat_map(Result::err).collect())
}
}
See also:
Why does `Option` support `IntoIterator`?
An imperative solution is often the most expressive and efficient way to implement some algorithm. It's Rust, not Haskell; not everything needs to be functional.
fn scan_tokens(source: &str) -> Result<Vec<Token>, Vec<ScanError>> {
let iter = Tokens {
chars: source.chars(),
};
let mut tokens = Vec::new();
let mut errors = Vec::new();
for result in iter {
match result {
Ok(token) => {
tokens.push(token);
}
Err(e) => {
errors.push(e);
}
}
}
if errors.is_empty() {
Ok(tokens)
} else {
Err(errors)
}
}
I have a function that calls another function which returns a Result. I need to check if the Result is Ok or Err and if it is an Err, I need to return early from my function. This is what I'm doing now:
match callable(&mut param) {
Ok(_v) => (),
Err(_e) => return,
};
Is there a more idiomatic Rust way to do this?
You can create a macro:
macro_rules! unwrap_or_return {
( $e:expr ) => {
match $e {
Ok(x) => x,
Err(_) => return,
}
}
}
fn callable(param: &mut i32) -> Result<i32, ()> {
Ok(*param)
}
fn main() {
let mut param = 0;
let res = unwrap_or_return!(callable(&mut param));
println!("{:?}", res);
}
Note that I wouldn't recommend discarding the errors. Rust's error handling is pretty ergonomic, so I would return the error, even if it is only to log it:
fn callable(param: &mut i32) -> Result<i32, ()> {
Ok(*param)
}
fn run() -> Result<(), ()> {
let mut param = 0;
let res = callable(&mut param)?;
println!("{:?}", res);
Ok(())
}
fn main() {
if let Err(()) = run() {
println!("Oops, something went wrong!");
}
}
If both functions return Result<doesn't matter, same T> you can just put a ? at the end of line of call.
fn caller() -> Result<Str, i32> {
let number = job()?; // <-- if job return error this function return/end here
// otherwise the value of Ok will assign to number
Ok(format!("the number is {}", number))
}
fn job() -> Result<i32, i32> {
// do something
Err(3)
}
You can use same pattern for Option<T> too.
You can use my unwrap_or crate to accomplish this.
You can do:
unwrap_or_ok!(callable(&mut param), _, return);
And if you want the result and to return the error, you can do:
let v = unwrap_or_ok!(callable(&mut param), error, return error);
Rust 1.65.0 has stabilized let-else statements, which enables you to write:
let Ok(_v) = callable(&mut param) else { return };
I want to return an error from a function in case a condition is true:
use std::error::Error;
pub fn run() -> Result<(), Box<dyn Error>> {
// -- snip ---
if condition {
// return error
}
// -- snip --
Ok(())
}
fn main() {}
I probably don't have the basics of the typesystem down, but everywhere I looked people use the ? operator, so I can't figure out what type to return.
Is it possible to just return an error like this?
Is there a better way to handle this logic?
Error is a trait and you want to return a trait object (note the dyn keyword), so you need to implement this trait:
use std::error::Error;
use std::fmt;
#[derive(Debug)]
struct MyError(String);
impl fmt::Display for MyError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "There is an error: {}", self.0)
}
}
impl Error for MyError {}
pub fn run() -> Result<(), Box<dyn Error>> {
let condition = true;
if condition {
return Err(Box::new(MyError("Oops".into())));
}
Ok(())
}
fn main() {
if let Err(e) = run() {
println!("{}", e); // "There is an error: Oops"
}
}
Create your own error type,
Implement Debug, Display, then Error for it,
If there is an error, return the Err variant of Result.
I advise you to use failure that remove all the error boilerplate:
#[derive(Fail, Debug)]
#[fail(display = "There is an error: {}.", _0)]
struct MyError(String);
--
Note that if you expect an Error, you can return whatever type you want, given that it implements Error. This includes the error types in std.
To return custom errors, given that the function is set to return Result<(), Box<dyn Error>>:
fn serve(config: &Config, stream: TcpStream) -> Result<(), Box<dyn Error>> {
// ...
if request_is_bad() {
// This returns immediately a custom "Bad request" error
Err("Bad request")?;
}
// ...
}
Box<dyn Error> is handy for types that implement it:
use std::error::Error;
fn main() -> Result<(), Box<dyn Error>> {
Err("March")?
}
but surprisingly, it doesn't work with all types:
use std::error::Error;
fn main() -> Result<(), Box<dyn Error>> {
// the trait `std::error::Error` is not implemented for `{integer}`
Err(9)?
}
as a workaround, you can use what I call the Error Format idiom:
use std::error::Error;
fn main() -> Result<(), Box<dyn Error>> {
Err(format!("{}", 9))?
}
Note this has many variations, for example with literals you can do this:
use std::error::Error;
fn main() -> Result<(), Box<dyn Error>> {
Err(concat!(9))?
}
and also, you may not even need to use Box<dyn Error>:
fn main() -> Result<(), String> {
Err(concat!(9))?
}
It can also be useful in cases where you normally don't need it. For example, this example below could work without it, but it's useful as it adds the filename to the error, which normally isn't shown:
use std::fs;
fn main() -> Result<(), String> {
let s = "a.rs";
match fs::read_to_string(s) {
Ok(v) => print!("{}", v),
// normal message is just: The system cannot find the file specified
Err(v) => Err(format!("{} {}", s, v))?
}
Ok(())
}
A Result<T, E> is an enum with two variants. To return either of them, you just use corresponding variants.
fn foo(var: bool) -> Result<(), i32> {
if var {
Ok(()) //in fact this is std::result::Result::Ok
} else {
Err(-3) //in fact this is std::result::Result::Err
}
}
The reason why you don't have to write std::result::Result::Ok is that it is in the prelude. As you can see, you don't have to stick to Box<Error>, but can return any type you want. It is a generic enum, with no restrictions.
The ?-operator is a handy shortcut for early returns, so you don't have to be too verbose about results.
I have following code. It works.
But i am more interested in writing make_tea where i call two functions : get_milk_from_cow and pour_milk. They both return Result<String, TeaError>.
How can i compose them so that i can keep concating Strings if they succeed otherwise return error.
enum TeaError {
NoMilk,
NoCup,
NoCow,
NoGas,
NoSomething,
}
fn get_milk_from_cow(cow: bool) -> Result<String, TeaError> {
if cow {
Ok(String::from("get milk from cow"))
} else {
Err(TeaError::NoCow)
}
}
fn pour_milk(milk: bool) -> Result<String, TeaError> {
if milk {
Ok(String::from("poured milk"))
} else {
Err(TeaError::NoMilk)
}
}
fn make_tea() -> Result<String, TeaError> {
let mut process = String::new();
let step_cow = get_milk_from_cow(true)?;
let step_milk = pour_milk(true)?;
process.push_str(step_cow.as_str());
process.push_str(step_milk.as_str());
Ok(process)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn attemp_tea_one() {
match pour_milk(false) {
Err(v) => match v {
TeaError::NoMilk => assert!(true),
_ => assert!(false)
},
Ok(_) => assert!(false)
};
}
#[test]
fn attemp_tea_two() {
match make_tea() {
Err(_) => assert!(false),
Ok(_) => assert!(true)
};
}
}
I tried :
process.push_str(get_milk_from_cow(true)?
.push_str(pour_milk(true)?.as_str()))
but it gives :
error[E0308]: mismatched types
--> src/errors.rs:27:22
|
27 | process.push_str(get_milk_from_cow(true)?.push_str(pour_milk(true)?.as_str()));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected &str, found ()
|
= note: expected type `&str`
found type `()`
as push_str does not return string.
Edit:
fn append(s1: String, s2: String) -> String {
s1 + s2.as_str()
}
fn make_tea() -> Result<String, TeaError> {
let process = append(get_milk_from_cow(true)?,
pour_milk(true)?);
Ok(process)
}
so, i can do append(append(funcA(), funcB()), funcC()) and so on..
I am still learning about lifetimes and think about whether append can still be improved in memory allocation.
This code does redundant work on the starred lines:
fn make_tea() -> Result<String, TeaError> {
* let mut process = String::new();
let step_cow = get_milk_from_cow(true)?;
let step_milk = pour_milk(true)?;
* process.push_str(step_cow.as_str());
process.push_str(step_milk.as_str());
Ok(process)
}
process.push_str(step_cow.as_str()) just makes an unneeded copy of step_cow. Instead, try
fn make_tea() -> Result<String, TeaError> {
let mut process = get_milk_from_cow(true)?;
process.push_str(&pour_milk(true)?);
Ok(process)
}
or, more conveniently,
fn make_tea() -> Result<String, TeaError> {
Ok(get_milk_from_cow(true)? + &pour_milk(true)?)
}
First things first: there appears to be nothing wrong with your code above, but I'll be assuming that you are looking for something more idiomatic.
Although requiring slightly more memory than your approach, the most elegant way to concatenate results of strings is this:
fn make_tea() -> Result<String, TeaError> {
vec![get_milk_from_cow(true), pour_milk(true)].into_iter()
.collect()
}
Playground
Explanation:
The vector is consumed into an iterator of Result<String, TeaError> (owned, not references).
collect then relies on two implementations of FromIterator:
impl<A, E, V> FromIterator<Result<A, E>> for Result<V, E>
where V: FromIterator<A> produces a result containing either the result of building A from an iterator of T or the first error retrieved from the iterator. This is like turning an iterator of results into a result with a collected iterator.
impl FromIterator<String> for String concatenates all strings into an owned string
So, as soon as you already have an iterator that turns your process into a sequence of independent operations, you can just collect them.
Now, in order to prevent subsequent operations from being called after an error is found, then it's easier to stick to the ? operator.
fn make_tea() -> Result<String, TeaError> {
Ok(vec![get_milk_from_cow(true)?, pour_milk(true)?].into_iter()
.collect())
}
Playground
The vector had to be created because arrays do not provide iterators of owned elements (&T instead of T). However, we can go around that with an extra mapping:
fn make_tea() -> Result<String, TeaError> {
Ok([get_milk_from_cow(true)?, pour_milk(true)?].into_iter()
.map(|a| a.as_str())
.collect())
}
This will map elements from &String into &str, which can be collected likewise.