Vector3 doesn't appear as a type

I can create a Vector3, but cannot create a struct containing a Vector3 nor functions which accept a Vector3.

Works as expected:

use nalgebra::Vector3;

main() {
    let _x = Vector3::new(1,2,3);

doesn’t work:

use nalgebra::Vector3;

fn bar(foo : Vector3) {} //expected 1 type argument. Why is Vector3 not recognised?

Doesn’t work:

use nalgebra:: Vector3;

struct foo {
    bar : Vector3 //again, fails to recognise a type

Just… why???


The error expected 1 type argument tells you that Vector3 is a generic type that needs to be parametrized by one type argument. You may be interested to read the Rust documentation about generics.

Here the type argument expected by Vector3 is the type of its components (x, y, z). For example if you want a 3D vector of floats, you write bar: Vector3<f32>. For a 3D vector of 32-bits unsigned integers, you can write bar: Vector3<u32>.

The reason why let _x = Vector3::new(1,2,3); works without any more type annotation is because the compiler is able to automatically infer the type argument of Vector3 from the arguments of new.


Why is it declared as a type then? How tf do you tell in Rust what’s a generic and what’s an actual type?

A generic type expects type parameters so it has these < > at the end of their name, for example, Vec<T>, HashMap<K, V>. See the aforementioned part of the Rust book.

Perhaps you are confused by the declaration type Vector3<N> = VectorN<N, U3>; which uses the type keyword? The type keyword defines a type alias. And this type alias can be generic too, which is visible here because of the <N> part of the alias. For example a non-generic type alias for float vectors could be type FloatVector3 = VectorN<f32, U3>.