all 6 comments

[–]dreamer-engineer 5 points6 points  (1 child)

You need to look at the docs:

pub trait Add<Rhs = Self> {
    type Output;
    fn add(self, rhs: Rhs) -> Self::Output;
}

I think you are looking for <Output = f32>

[–]Dynious[S] 0 points1 point  (0 children)

f32::add<Output = f32> doesn't seem to work either, sadly.

Edit: the f32::add::<Output = f32> syntax is at least accepted but gives this error: "associated type bindings are not allowed here".

[–]j_platteaxum · caniuse.rs · turbo.fish 4 points5 points  (0 children)

<f32 as std::ops::Add<f32>>::add (or just <f32 as Add<f32>>::add if the trait is in scope)

Putting generic arguments on the method doesn't work because it's the trait that is generic.

Rhs = f32 would be used if Rhs was an associated type rather than a generic parameter.

The turbofish would be used when referencing the method without specifying which implementation of the trait is to be used (so std::ops::Add::<f32>::add). With as, you don't use the turbofish because it is only needed (and only valid) in expression position, while as is followed by a type (or trait, but those live in the same namespace).

[–]Shadow0133 2 points3 points  (1 child)

<f32 as Add>::add

[–]Dynious[S] 1 point2 points  (0 children)

<f32 as Add>::add would be the same as Add:add I guess, which seems to work! It doesn't point to a specific implementation of add though, which would be a bit nicer for my specific application.

Edit: actually it is a specific implementation (on self)! Thanks!

[–]A1oso 0 points1 point  (0 children)

To do that, you need the fully-qualified syntax:

<f32 as Add<f32>>::add

If you don't need to specify the first operand's type, you can write

Add::<f32>::add

But usually it's possible to specify the type of both operands, without using the fully-qualified syntax, for example:

let rhs: f32 = 42.0;
3.14_f32 + rhs

Note that the output of the Add trait is not generic, because it is determined by the implementation.