2

In this example, the compiler can not infer the matrix type:

type Mat4x4<T> = [T; 16]; fn main() { let m: Mat4x4 = [0.4323f32; 16]; println!("{:?}", m); } 

The working code is:

type Mat4x4<T> = [T; 16]; fn main() { let m: Mat4x4<f32> = [0.4323f32; 16]; println!("{:?}", m); } 

Is this an expected act?

2 Answers 2

6

This is not a type inference issue:

type Mat4x4<T> = [T; 16]; fn main() { let m: Mat4x4 = [0.4323f32; 16]; println!("{:?}", m); } 

Yields the following error message:

error[E0107]: wrong number of type arguments: expected 1, found 0 --> src/main.rs:4:12 | 4 | let m: Mat4x4 = [0.4323f32; 16]; | ^^^^^^ expected 1 type argument 

The complaint here is that Mat4x4 is not a type, it's a template or blueprint to create a type.

An analogy would be that Mat4x4 is a waffle iron, and Mat4x4<f32> is a waffle that comes out of it. If you are served the waffle iron (with maple syrup on top, of course) you will likely be disappointed!

The same applies here: when you give the compiler the blueprint where it expects the final product, it signals you that it was not what it expected.


You can supply a dummy argument (_), and it will be inferred:

let m: Mat4x4<_> = [0.4323f32; 16]; 
Sign up to request clarification or add additional context in comments.

1 Comment

I liked your waffle example. :D
4

You cannot omit required type parameters, but you can use _ to infer them:

let m: Mat4x4<_> = [0.4323f32; 16]; 

Alternatively, you could add a default type parameter so you could omit the <…> when the type T is exactly f32 (but this is not type inference, you still need to write Mat4x4<f64> explicitly).

type Mat4x4<T = f32> = [T; 16]; 
let m: Mat4x4 = [0.4323f32; 16]; 

2 Comments

In most of the times you don't need to use '_' for inferencing, is this something expected in here?
@HosseinNoroozpour: You mean when you infer the whole type, e.g. let m = [0.4f32; 16];? But here you are restricting it to a Mat4x4<T>.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.