问题描述
我试图在Rust中编写一个if语句,只要get的结果不是(nil),它就可以在条件语句中成功。像这样:if database :: get(variable)!=(nil){}我要为(nil)传递什么?
解决方法
我假设您是指Optional
?您只需编写database::get(variable).is_some()
或.is_none()
。
但是,在更可能的情况下,您想使用变量中的值:
if let Some(variable_unwrapped) = database::get(variable) {
// Now you can use the `variable_unwrapped` variable
}