You can use Match guards.
let string = match current % 2 {
even if even == numbers[0] => "even",
odd if odd == numbers[1] => "odd",
_ => unreachable!()
};
More Related Contents:
- How does Rust pattern matching determine if the bound variable will be a reference or a value?
- How can the ref keyword be avoided when pattern matching in a function taking &self or &mut self?
- How can I pull data out of an Option for independent use?
- Why can’t I store a value and a reference to that value in the same struct?
- How do I create a heterogeneous collection of objects?
- Is it possible to control the size of an array using the type parameter of a generic?
- How do I stop iteration and return an error when Iterator::map returns a Result::Err?
- Are polymorphic variables allowed?
- Does println! borrow or own the variable?
- Why does Iterator::take_while take ownership of the iterator?
- Efficiently mutate a vector while also iterating over the same vector
- Why is it possible to implement Read on an immutable reference to File?
- Why do I need rebinding/shadowing when I can have mutable variable binding?
- How do I use the Entry API with an expensive key that is only constructed if the Entry is Vacant?
- How do I split a string in Rust?
- How can I put an async function into a map in Rust?
- Is it possible to implement methods on type aliases?
- How to end a borrow in a match or if let expression?
- How do I synchronously return a value calculated in an asynchronous Future?
- How do I move out of a struct field that is an Option?
- Why does creating a mutable reference to a dereferenced mutable reference work?
- Cannot borrow `x` as mutable more than once at a time
- Removing elements from a Vec based on some condition [duplicate]
- How can I change enum variant while moving the field to the new variant?
- What is unwrap in Rust, and what is it used for?
- What’s the difference between using the return statement and omitting the semicolon in Rust?
- The trait `FnMut` is not implemented for `String` when trying to split a string
- HashMap borrow issue when trying to implement find or insert
- Why “explicit lifetime bound required” for Box in struct?
- Why do I get “does not live long enough” in a return value?