0

I am new to rust, and am missing the if let and guard let notation in Swift.

I have the following code block:

fn some_function() -> Option<String> {
  let client = reqwest::Client::new();

  // res is normally a response type. I've mapped it to an Option type to drop the error
  let res = client
    .post(&complete_path)
    .basic_auth(self.username.clone(), Some(self.password.clone()))
      .send()
      .ok();

  // Verify status code and exist if res = None, or status !== 200
  let mut response = match res {
    Some(res) => match res.status() {
      StatusCode::OK => res,
      _ => return None
    },
    _ => return None
  };

  // Attempt to read the body contents
  let token = match response.text() {
    Ok(data) => Some(data),
    Err(_e) => None
  };

  token
}

In swift I would have written something like:

guard let response = response,
  response.status == 200
  let text = response.text() else {
  return None
}
return text

Am I missing some shorthand notation?

I'm trying to make use of the ability to return from anywhere with return to short circuit some of the execution, but it's still much more verbose than I am familiar with.


EDIT:

I can collapse some of the rust with the match + clause syntax as below:

let mut response = match res {
  Some(res) if res.status() == StatusCode::OK => res,
  _ => return None
}

That is much better than the original.

If let also works, but the problem with if let here is that I'm looking at the failure path here. I do not want to nest in on the happy path.

4

1 回答 1

1
于 2020-07-15T21:50:05.323 回答