0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Rust EnumとResult, Option型について

Posted at

Enumについて

rustを勉強してたらまた知らない概念が enum

わかりやすい記事1
わかりやすい記事2

Result型とOption型について

Result型

Option型

定義

Option は、「値があるかもしれないし、ないかもしれない」という状態を表す型です。

enum Option<T> {
    Some(T),
    None,
}

fn find_user(id: u32) -> Option<&'static str> {
    match id {
        1 => Some("Alice"),
        2 => Some("Bob"),
        _ => None,
    }
}

fn main() {
    let user = find_user(1);
    match user {
        Some(name) => println!("User found: {}", name),
        None => println!("User not found"),
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?