HiveBrain v1.2.0
Get Started
← Back to all entries
snippetrustCritical

How do I conditionally execute code only when an Option is None?

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
conditionallyhownoneoptiononlywhenexecutecode

Problem

I don't want to take action if the function returns a value in the optional; how do I test the None case only? This code works, but it looks horrible.

let v = ffunc();
match v {
  None => { callproc() }, 
  Some(x) => {  }
}


In C, I can write:

int x = ffunc();
if ( !x ) { callproc() }

Solution

If you are not interested in the value, just use Option::is_none(), or its counterpart Option::is_some():

if v.is_none() { ... }

Code Snippets

if v.is_none() { ... }

Context

Stack Overflow Q#53177980, score: 102

Revisions (0)

No revisions yet.