gpt4 book ai didi

rust - 如何处理 "multiple applicable items in scope"错误?

转载 作者:行者123 更新时间:2023-12-03 11:28:28 28 4
gpt4 key购买 nike

我正在使用 fltk-rs crate 并遇到“范围内的多个适用项目”错误。

fltk = "0.10.14"
use fltk::{table::*};

pub struct AssetViewer {
pub table: Table,
}

impl AssetViewer {
pub fn new(x: i32, y: i32, width: i32, height: i32) -> Self {
let mut av = AssetViewer {
table: Table::new(x,y,width-50,height,""),

};
av.table.set_rows(5);
av.table.set_cols(5);
av
}
pub fn load_file_images(&mut self, asset_paths: Vec<String>){
self.table.clear(); //<- throws multiple applicable items in scope
}
}
给出错误:
error[E0034]: multiple applicable items in scope
--> src\main.rs:18:20
|
18 | self.table.clear(); //<- throws multiple applicable items in scope
| ^^^^^ multiple `clear` found
|
= note: candidate #1 is defined in an impl of the trait `fltk::TableExt` for the type `fltk::table::Table`
= note: candidate #2 is defined in an impl of the trait `fltk::GroupExt` for the type `fltk::table::Table`
我想说明我正在引用 TableExt特质,而不是 GroupExt特征。我该怎么做?

最佳答案

TLDR:使用完全限定的函数名称:

fltk::GroupExt::clear(&mut self.table)
考虑这个简化的例子:
struct Bar;

trait Foo1 {
fn foo(&self) {}
}
trait Foo2 {
fn foo(&self) {}
}
impl Foo1 for Bar {}
impl Foo2 for Bar {}

fn main() {
let a = Bar;
a.foo()
}
它将无法编译并显示以下错误消息:
error[E0034]: multiple applicable items in scope
--> src/main.rs:16:7
|
16 | a.foo()
| ^^^ multiple `foo` found
|
编译器还会提出一个解决方案:
help: disambiguate the associated function for candidate #1
|
16 | Foo1::foo(&a)
|

关于rust - 如何处理 "multiple applicable items in scope"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65375671/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com