How to handle the errors elegantly?

I have this line:

let square = self.squares.get_mut(index)
			.ok_or(Error::new(format!("Given {:?} is not valid", point))?;

It doesn't compile because &str doesn't implement Try trait. So, what I want is an Error which takes a &str input so that, when I use unwrap(), that string the will be displayed, when it panics or passed as Error when I use that '?'. The .expect() method came close but, it panics instead of passing it as result

What is Error in this code?

1 Like

self.squares is just an array. self.squares.get_mut(index)?. So, if it gives an error it gives NoneError which I don't want. What I want is an Error object which takes string sort of like this(.ok_or(Error::new(format!("Given {:?} is not valid", point)))?. This error should be put into it result object

Why there are no replies? :worried: Did I do something wrong?

I think you can just remove Error::new(...) and leave the format:

let square = self.squares.get_mut(index) 
    .ok_or(format!("Given {:?} is not valid", point))?;

Where x is ment as the variable you pass to format. not mentioned in your OP.

But, it will ask the Module from which the Error needs to be imported? There is only two concrete error types(fmt::Error, io::Error) in std library as far as I know, but both of them cannot be used here.
About that error

Its not point", you've added extra double inverted commas. Its just point. Because the point is here the variable.
Anyway, thanks for the reply. I thought the post was muted or something

Right, my bad, have updated my answer.

You should post the signature of your function or method, and the declaration of the return error if it is not clear which kind of error you should return.

This is is my function signature (

pub fn insert(&mut self,point: Point, player: Player) -> Result<(), SomeErrorType>

). I want an Error type that basically takes string like this(Error::new("reason of error")). So, that when I use unwrap() instead instead of handling it, it will display that reason when it panics.

Could you please format the code properly to help readability?

I think you should create a SomeErrorType object, you should post the definition of this type please.

Thanks for telling me. I formatted the code as you said.
Anyway, about that SomeErrorType. I don't know that SomeErrorType. That's why I was asking in this post. Basically,

The Result<T, E> type take 2 types, so the error can be any give type you want:

fn my_func() -> Result<(), String> {
    if something_wrong() {
        return Err(String::from("....error string"));

In this example I use a plain String to send back Errors, the way it recognize it is an error, is by wrapping with the Err() instead of the Ok().

To have a more elegant way you should define your own error type, that can be a struct or an enum.

You can start to use an enum:

enum MyError {

fn my_func() -> Result<(), MyError> {
    if something_wrong() {
        return Err(MyError::GenericError);

In this case you should implement on the MyError enum all the logic and methods you need to converts from other error types or to print (implement Display), or constructors/builders.

When you work on a crate that will be used on other projects, you can even provide a trait to allow people to customize the errors.

1 Like

Thanks for the help. I don't know why it didn't occur to me to just use enums. It was so obvious.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.