Ask coding questions

← Back to all posts
What is Ok() in Rust?
h
MocaCDeveloper (649)

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!

Comments
hotnewtop
Coder100 (18057)

wait a second this is covered in the official tutorial what

Coder100 (18057)

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

MocaCDeveloper (649)

@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).