Rust "if let"不工作?

标签 rust

我将 libxml2 包装在 Rust 中作为学习 Rust FFI 的练习,我遇到了一些奇怪的事情。我是 Rust 的新手,但我相信以下内容应该有效。

main.rs我有:

mod xml;

fn main() {
    if let doc = xml::parse_file("filename") {
        doc.simple_function();
    }
}

xml.rs是:

extern create libc;

use libc::{c_void, c_char, c_int, c_ushort};
use std::ffi::CString;

// There are other struct definitions I've implemented that xmlDoc
// depends on, but I'm not going to list them because I believe
// they're irrelevant

#[allow(non_snake_case)]
#[allow(non_camel_case_types)]
#[repr(C)]
struct xmlDoc {
    // xmlDoc structure defined by the libxml2 API
}


pub struct Doc {
    ptr: *mut xmlDoc
}

impl Doc {
    pub fn simple_function(&self) {
        if self.ptr.is_null() {
            println!("ptr doesn't point to anything");
        } else {
            println!("ptr is not null");
        }
    }
}

#[allow(non_snake_case)]
#[link(name = "xml2")]
extern {
    fn xmlParseFile(filename: *const c_char) -> *mut xmlDoc;
}

pub fn parse_file(filename: &str) -> Option<Doc> {
    unsafe {
        let result;
        match CString::new(filename) {
            Ok(f) => { result = xmlParseFile(f.as_ptr()); },
            Err(_) => { return None; }
        }
        if result.is_null() {
            return None;
        }
        Some(Doc { ptr: result })
    }
}

我正在包装 C 结构,xmlDoc在一个很好的 Rust 友好结构中,Doc , 以明确区分安全 (Rust) 和不安全 (C) 数据类型和函数。

这在大多数情况下都有效,除了当我编译时,我在 main.rs 中遇到错误:

src/main.rs:38:13: 38:28 error: no method named 'simple_function' found
for type 'std::option::Option<xml::Doc>' in the current scope
src/main.rs:38         doc.simple_function();
                       ^~~~~~~~~~~~~~~
error: aborting due to previous error`

似乎确信 doc是一个 Option<xml::Doc>即使我使用的是 if let应该打开 Option 的表格类型。我做错了什么吗?

match xml::parse_file("filename") {
    Some(doc) => doc.simple_function(),
    None => {}
}

以上工作正常,但我想使用 if let如果可以的话,Rust 的特性。

最佳答案

您需要将实际模式传递给 if let(不像 Swift 这样的语言,if letOption 类型的特殊情况):

if let Some(doc) = xml::parse_file("filename") {
    doc.simple_function();
}

关于Rust "if let"不工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38670303/

相关文章:

c - 如何从tree-sitter中的节点获取值?

for-loop - 如何循环特定(可变)次数?

rust - 异步函数返回的 Future 的生命周期

rust - 在非词句生存期内,何时引用是 'used'?

multithreading - 使Rust线程退出阻塞操作的标准方法是什么?

rust - 如何确定可以格式化的字体的打印宽度?

recursion - 什么时候可以在Rust中保证尾递归?

rust - 错误: Failed to run custom build command for `gmp-mpfr-sys v1.4.0`

rust - 了解 rust 借用和取消引用

rust - 为什么 `Idx` 特征的 `Index` 类型参数允许取消大小?