此问题在此处已有答案:
Why do I need to import a trait to use the methods it defines for a type?(1个答案)
19天前关闭。
我试图做简单的结构与几个特征以下的例子在互联网上遇到了以下问题。
//My struct in framebuffer.rs
pub struct HighLevelFrameBuffer<'a> {
pub fbinfo: FrameBufferInfo,
pub fbdata: &'a mut [u8],
pub pixel_bytesize: u8,
pub color: FrameBufferColor,
}
//Trait and implementation
trait WriteLine {
fn write_line(&self, line: usize, color: FrameBufferColor);
}
impl<'a> WriteLine for HighLevelFrameBuffer<'a> {
fn write_line(&self, line: usize, color: FrameBufferColor) {
//Do stuff
}
}
当我尝试从我的主函数调用它时,如下所main.rs:
mod framebuffer;
pub use framebuffer::HighLevelFrameBuffer;
pub use framebuffer::FrameBufferColor;
let hlfb = HighLevelFrameBuffer{
fbinfo: something,
fbdata: data_location,
pixel_bytesize: 0,
color: FrameBufferColor {r: 0, g: 0, b: 0}
};
hlfb.write_line(100, FrameBufferColor{r: 0xFF, g: 0xFF, b: 0xFF});
编译器告诉我没有实现HighLevelFrameBuffer结构的write_line:
hlfb.write_line(100, FrameBufferColor{r: 0xFF, g: 0xFF, b: 0xFF});
| ^^^^^^^^^^ method not found in `HighLevelFrameBuffer<'_>`
pub struct HighLevelFrameBuffer<'a> {
| ----------------------------------- method `write_line` not found for this struct
我在这里做错了什么?
1条答案
按热度按时间eni9jsuy1#
这可能有几个原因:
1.您是否在输入
write_line
时出现了打字错误?WriteLine
是否定义在与调用hlfb.write_line
不同的文件中?如果是,那么您是否添加了use
语句来导入WriteLine
?没有更多的细节,很难说得更具体。