How can I return the first non-empty string?

Viewed 1355

The following Python code returns the first non-empty string (in this example, the content of bar):

foo = ""
bar = "hello"
foo or bar # returns "hello"

How do I write it in Rust? I tried with this:

let foo = "";
let bar = "";
foo || bar;

but I am getting this

error[E0308]: mismatched types
 --> src/main.rs:4:5
  |
4 |     foo || bar;
  |     ^^^ expected bool, found &str
  |
  = note: expected type `bool`
             found type `&str`

I suppose I can't easily do what I do in Python with Rust?

4 Answers
Related