Skip to content
Sign UpLog In
This post is read-only. Explore Repls and connect with other creators on Community.View Community
The info in this post might be out of date, check out our docs instead. View docs
1

What is Ok() in Rust?

MocaCDeveloper
MocaCDeveloper

Hi!

I am currently learning Rust to get into some top-notch security application development, and I have ran into this concept of Ok() and Err() in Rust.

Explanation of what I know

Well, I know absolutely not a SINGLE thing about them.
I understand the concept of Some() and None, as you can see in the code below. But I do not understand the concept of Result, Ok, and Err.

If there are any Rust developers out there, could you possibly explain this to me?
And please please please DO NOT give me links to documentation, lmao. I have spent the last hour and a half researching this topic and none of the documentation I have read explains how to use it. They mainly describe the functionalities behind it, which is something that is not quite useful.

I want to know:

  1. How to use it
  2. When to use it
  3. What it's used for
  4. Why I should use it
  5. The functionalities for it

So, if there are any Rust developers that could explain this concept, please do so. It would mean allot!

2 years ago

Voters

Comments

TopNew
1
Coder100
Coder100

wait a second this is covered in the official tutorial what

2 years ago
1
Coder100
Coder100

Rust doesn't have errors, instead it has a enum.

enum Result<T, E> { Ok(T), Err(E) }

https://doc.rust-lang.org/std/result/enum.Result.html

so basically then you can just check the result, if it was an Err type then you handle the error otherwise you use the output. Pretty elegant i think

2 years ago
1
MocaCDeveloper
MocaCDeveloper

@Coder100

Ahh. So what I am getting from this is if the result is valid, then it is Ok(T), otherwise it will error and then it will be Err(e).

2 years ago
Load more