I'm writing a little math library to start applying some of the Rust concepts I've learned from reading the documentation, and I've hit a snag. I've defined this trait in my math module:
pub trait TransformPoint<T> {
fn transform_point(&self, point: &T) -> T;
}
I've implemented for my Mat2
type like so:
impl math::TransformPoint<Vec2> for Mat2 {
/// Find the resulting vector given a vector and matrix
///
/// # Examples
/// ```
/// use vex::Mat2;
/// use vex::Vec2;
/// let m = Mat2::construct(1.0, 2.0, 3.0, 4.0);
/// let v = Vec2::construct(1.0, 2.0);
/// let actual = m.transform_point(&v);
/// let expected = Vec2::construct(7.0, 10.0);
/// assert_eq!(actual, expected);
/// ```
fn transform_point(&self, point: &Vec2) -> Vec2 {
Vec2::construct(
self.m11() * point.x + self.m12() * point.y,
self.m21() * point.x + self.m22() * point.y,
)
}
}
I can build it just fine with cargo build
, but my documentation test fails with this error stating that transform_point()
isn't defined for Vec2
:
---- src/mat2.rs - mat2::Mat2::transform_point (line 614) stdout ----
error[E0599]: no method named `transform_point` found for type `vex::Mat2` in the current scope
--> src/mat2.rs:619:16
|
8 | let actual = m.transform_point(&v);
| ^^^^^^^^^^^^^^^
|
= help: items from traits can only be used if the trait is in scope
help: the following trait is implemented but not in scope, perhaps add a `use` for it:
|
3 | use vex::math::TransformPoint;
|
Importing the trait as the error output suggests resolves the error, but I'm not sure why that is. It sounds like merely importing a type via the use
syntax isn't enough to have access to all of its implemented methods if they're associated with traits. If I want to directly call a trait method defined for my type that I've already imported, I'll need to make sure that my current scope also imports that trait as well. Is that correct?