【问题标题】:Method from included Trait implementation not found in scope in Rust在 Rust 的范围内找不到包含的 Trait 实现中的方法
【发布时间】:2020-09-11 08:05:27
【问题描述】:

我想使用两个外部库(geo-types-0.6.0 和 geo-offset-0.1.0)来执行几何算法。

下面的例子看起来不错: Line 类型在库 geo_types 中定义。 Offset 特征还写在 geo_offset 中。包含这个特征应该导致Line 类型实现方法offset。 但是我收到以下错误:

no method named `offset` found for struct `geo_types::line::Line<float>` in the current scope

除此之外,VS Code 中的 rust-analyzer 告诉我,包含的特征 Offset 没有被使用。这是为什么呢?

use geo_types::{Coordinate, Line};
use geo_offset::Offset;

let line = Line::new(
    Coordinate { x: 0.0, y: 0.0 },
    Coordinate { x: 1.0, y: 8.0 },
);

let line_with_offset = line.offset(2.0)?;

【问题讨论】:

    标签: rust compiler-errors


    【解决方案1】:

    geo-offset crate 实现了geo::LineOffset 特征,而不是geo_types::Linesrc - 搜索geo::Line)。所以即使geo::Line 只是geo_types::Line 的重新导出,rust 编译器也看不到这么深,只知道geo::LineOffset 实现。

    【讨论】:

    • 也许是因为关于地理偏移的示例是错误的,并且显示了您所做的事情?可能值得向他们提交错误...
    • @Nevsden 如果解决了问题,也习惯性地接受答案。这表明“这个问题已经解决了”。
    • 再次感谢,刚刚将您的答案标记为解决方案。对于您的其他评论:是的,可能是这样,我会报告它。
    猜你喜欢
    • 1970-01-01
    • 2021-06-29
    • 1970-01-01
    • 2022-08-18
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 2023-03-26
    • 1970-01-01
    相关资源
    最近更新 更多