【发布时间】:2017-03-22 05:48:30
【问题描述】:
我想在结构I 上调用方法a。我被告知 Rust 找不到该方法,但不确定原因:
error: no method named `a` found for type `*mut I` in the current scope
--> src/lib.rs:7:16
|
7 | unsafe { i.a(5) }
| ^
这是一个最小的可重现示例:
extern crate libc;
use self::libc::int32_t;
#[no_mangle]
pub extern "C" fn i_a(i: *mut I) -> *mut int32_t {
unsafe { i.a(5) } // error: no method named `a` found for type `*mut I` in the current scope
}
#[derive(Debug, PartialEq)]
pub struct I {
pub values: Vec<i32>,
}
impl I {
pub fn a(&self, n: i32) -> i32 {
return 0;
}
}
我该如何解决这个问题?
【问题讨论】:
-
解引用指针,不像引用,不是隐式的。您必须取消引用该指针。
-
旁注:您的
i_a函数返回*mut int32_t而a方法返回i32,即使您解决了问题,类型也不会匹配。
标签: rust