我正在尝试使用cgmath库。我有以下main.rs文件:
extern crate cgmath;
use cgmath::vector::{Vector3, EuclideanVector};
fn main() {
let mypoint = Vector3 { x: 1f64, y: 1f64, z: 3f64 };
println!("The length of the vector is {}, and the length squared is {}", mypoint.length(), mypoint.length2());
}
在我的使用中,当我省略
EuclideanVector
时,会出现以下编译错误:type 'cgmath::vector::Vector3<f64>' does not implement any method in scope named 'length'
看起来 Rust 编译器无法找到 length() 方法,除非我导入 Vector3 使用的 traits 之一。深入 源代码,似乎 length 方法在 EuclideanVector trait 中定义。
直觉上,我不应该需要导入一个 trait 来使用继承了该 trait 的类型。我是否缺少某种技巧?这是 cgmath 库特有的细微差别吗?这是 Rust 的惯用方式,我应该习惯这样做吗?