Minor confusion about `_` pattern in a function parameter

⚓ Rust    📅 2025-06-22    👤 surdeus    👁️ 9      

surdeus

Warning

This post was published 52 days ago. The information described in this article may have changed.
fn main(){
  let x = String::from("hello");
  fn meow(_:String){
      
  }
  let _:String = x; // no move
  // wo `let` it is an underscore expression though
  _ = x; // no move
//meow(x); // move
  x;
}

Why does the pattern throw the value away in a function call?

The reference was to an extent confusing for me (but I may have missed different behaviour on functions?):

Unlike identifier patterns, it does not copy, move or borrow the value it matches.

5 posts - 4 participants

Read full topic

🏷️ rust_feed