<T as Add>::Output
means "the associated type Output
of the trait Add
as implemented by T
". Associated types are types that trait implementations get to choose.
For Add
in particular, it means “the type of the result you get when you add a T
to a T
”. This is usually the same as T
but not always; for example, if you add an &i32
to an &i32
then you get an i32
, not another &i32
.
In this syntax, the angle brackets have to be positioned exactly where they are — it is always <SomeType as SomeTrait>::SomeAssociatedTypeName
. You can't move the angle brackets around or it will mean something else or be a syntax error.
the difference between ":Debug" and "::Debug"
These are completely different. The double colon ::
is for looking up things in namespaces (you've probably seen it in use
and calling functions) and in a type context, it is for referring to an associated type.
The single colon :
is for a bound. T: Tr
is a trait bound, meaning that the type T
must implement the trait Tr
. It is normally written with a space after the colon.
So, fn Printer ... where <T as Add>::Output: Debug
means that the function Printer
can only be called if T
's output when added to itself implements Debug
(can be printed with debug formatting).
And why "Math::new"
I'm not sure what you mean, but I'll try to answer: As I mentioned above, ::
is for looking up things in namespaces. So this looks for new
inside of Math
. Usually, this would mean that you have a type Math
with an associated function New
:
impl Math {
fn new(...) { ... }
}
Do you mean “why not Math.new
?” The answer to that question is that Rust makes a systematic distinction between the two: ::
is only used when the left side is a type, trait, module, or some other kind of “static” entity which is only determining a compile-time choice, and .
is only used when the left side is an expression — a struct field access, or a method call — that provides a run-time value for the operation to work on.
It gives an error when typing "Math::Printer" while it is running. Can you explain this difference?
In order to explain I would need to see what code you mean, and the full error message from cargo check
to understand what the situation is. General advice — in programming, there are lots of different ways a program can be wrong. It's almost always necessary to see the program and the error to help.