我将 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 let
是 Option
类型的特殊情况):
if let Some(doc) = xml::parse_file("filename") {
doc.simple_function();
}
关于Rust "if let"不工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38670303/