How to write "not if let"?


#1

I want check if “enum” not match some pattern, and in that case return error.
Result of match not interesting for me, at now I wrote:

    if let Some(&TokenTree::Token(_, Token::Semi)) = iter.next() {
    } else {
        return Err("expect ';' here".to_string());
    }

I dislike empty if part, idealy I want write something like this:

  if not (let Some(&TokenTree::Token(_, Token::Semi)) = iter.next()) {
      return Err("expect ';' here".to_string());
  }

I something like this possible?


#2

The solution for this would be “if let Err(err) = …”


#3

For Option and Result, there are helper methods for this:

  if iter.next().is_none() {
    // code 
  }
  if ret.is_err() {
    // code
  }

#4

It looks like you have several enums nested together though, in which case neither of the prior solutions really solve your specific problem. There’s no syntax for “does not match this one pattern.”

I would recommend using a match rather than if let else, though, because I think its less confusing:

match iter.next() {
     Some(&TokenTree::Token(_, Token::Semi)) => { }
     _ => return Error("expect ';' here".to_string())
}

You might also leave a comment in the ‘ok’ block to explain why its empty.


#5

Has there ever been any thoughts to having a guard let else syntax in Rust, similar to what is in Swift?


#6

It looks bad to me. This is not a part of Swift I’d like to see copied in Rust.

If you want to copy in Rust something from Swift, then I’d like parts like this:

That is, overflow checks removals, escape analysis to remove some boxing and heap allocation of objects and strings, devirtualization of some trait object usage, array bound test removals, and more of the same.


#7

I disagree.

This is succient and powerful and common.


#8

Mhm, I think I would like a mightier if let syntax in general. ‘if let … && …’ and so on. Or, in this case, if !let …
If let is sufficient most of the times, but feels somewhat restricted sometimes, and i don’t feel the need for that