In Rust I check if an Option has a value with is_some(). How do I do this in EK9?

← Getting Started · Ref: Q1004

In EK9, append ? after the variable name. It returns true if the value is set.

Rust: if my_option.is_some() { use(my_option.unwrap()) }
EK9: if myValue?

        use(myValue)

For Rust's 'if let Some(v) = expression' pattern, EK9 uses a guard:
Rust: if let Some(name) = find_user(id) { println!("{}", name); }
EK9: if name <- findUser(id)

        stdout.println(name)

The guard declares 'name' AND checks if findUser returned a set value. The block only runs if set — exactly like Rust's if let.

EK9 has no None/null/nil. Values are either absent, unset, or set. The ? suffix checks for set.

Example

defines module qa.gettingstarted.fromrustoption

  defines function

    findUser() as pure
      -> userId as Integer
      <- rtn as String: String()

      knownId <- 42
      if userId == knownId
        rtn: "Alice"

  defines program

    FromRustOptionDemo()
      stdout <- Stdout()

      // Like Rust: if let Some(name) = find_user(42)
      if name <- findUser(42)
        stdout.println(`Found: ${name}`)

      // Like Rust: if option.is_some()
      maybeUser <- findUser(99)
      if maybeUser?
        stdout.println(maybeUser)
      else
        stdout.println("Not found")

Common mistakes

E01073 — 'null' does not exist in EK9; use the tri-state '?' isSet operator (or an 'if name <- expr()' guard) instead. See ek9 -h E01073 for details.

Incorrect:

if maybeUser <> null

Correct:

if maybeUser?
Other ways to ask this
  • What is the EK9 equivalent of Rust's Option.is_some()?
  • I use Rust's if let Some(v) pattern. What does EK9 use?
  • How do I handle optional values coming from Rust to EK9?

Coming from another language?

Rust developers: ? suffix replaces is_some(), guard replaces if let Some(). No unwrap() needed — the guard guarantees the value is set inside the block.

Keywords: none, option, rust, migration, some, isSet, guard