What is the difference between `if let Some(ref x) = y` and `if let Some(x) = y.as_ref()`?

⚓ Rust    📅 2026-01-04    👤 surdeus    👁️ 4      

surdeus

Today I happened to write a code:

let y = Some("".to_string());
let closure = || {
  if let Some(ref x) = y
    && x.is_empty()
  {
    false
  } else {
    true
  }
}

And noticed that the closure is FnOnce, which seems odd since no ownership is taken.

Then I changed the if let line to:

  if let Some(x) = y.as_ref()

The closure now is Fn.

What is the difference here that gives this result?

5 posts - 3 participants

Read full topic

🏷️ Rust_feed