Why does rust complain of variable sometimes and sometimes does not complain

When I run this, rust complains (at the Some line) that l had already been moved at match statement
for l in buf_file.lines() { match re.find(&l.unwrap()) { Some(_) => println!("{}", l.unwrap()), None => (), } }

But if I run the code below there is no complaint
for l in buf_file.lines() { let ul = l.unwrap(); match re.find(&ul) { Some(_) => println!("{}", ul), None => (), } }
Should the statement for Some complaint that I moved ul at match? Note : buf_reader is a BufReader of a File and re is a Regex pattern. I am not able to picture what is going on with move in these cases.

In the first example the l.unwrap() moves l. You need to use l.as_ref().unwrap() to prevent the move. In the second example let ul = l.unwrap() also moves l, but you then only use ul and not l anymore.


This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.