Nevsden
Nevsden

Reputation: 129

Method from included Trait implementation not found in scope in Rust

I want to use two external libraries (geo-types-0.6.0 and geo-offset-0.1.0) to perform geometric algorithms.

The example below seems fine: The Line type is defined in the library geo_types. The Offset trait moreover is written in geo_offset. Including this trait should lead to the Line type implementing method offset. However I get the following error:

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

In addition to that, the rust-analyzer in VS Code tells me, that the included trait Offset is not used. Why is that?

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)?;

Upvotes: 3

Views: 914

Answers (2)

Paul
Paul

Reputation: 8182

This answer is wrong - see Chayim Friedmans answer for the correct answer.

The geo-offset crate implements the Offset trait for geo::Line, not geo_types::Line (src - search for geo::Line). So even though geo::Line is just a re-export of geo_types::Line, the Rust compiler doesn't see this deep and only knows about the Offset implementation for geo::Line.

Upvotes: -1

Chayim Friedman
Chayim Friedman

Reputation: 70970

The accepted answer is incorrect. Rust does see through reexports. The problem is that geo-offset uses geo v0.12.2, which in turn uses geo-types v0.4.2, which is incompatible with v0.6.0, so Cargo pulls two different version of geo-types, and they are incompatible with each other.

Downgrade your geo-types version to 0.4.2 and you should be fine.

Upvotes: 1

Related Questions