Can you explain these to me (help)

hi today i was making a simple math interpreter for experiment and the compiler told me to add this can you explain to me what exactly this line means?

fn Printer(&self) -> &Vec<T> where <T as Add>::Output:Debug

What is the difference between the above
fn Printer(&self) -> &Vec<T> where <T as Add + Debug>::Output::Debug
OR
fn Printer(&self) -> &Vec<T> where <T as Add + Debug::Output::Debug>

Also can you explain to me the difference between ":Debug" and "::Debug"

I know it uses a trait, but I've never seen this type of use before, can you explain exactly what's the difference?

And one last question

    let CreateObject = Math::new(
        vec![10, 20],
        '+',
    );

    println!("{:#?}", CreateObject.Printer());

And why "Math::new"
It gives an error when typing "Math::Printer" while it is running. Can you explain this difference?

<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.

6 Likes

For looking up basic syntax, please read The Book and search The Reference.

BTW: you seem to be trying to return a vec "by reference", but that is not what & means in Rust. & is a shared, temporary, scope-bound loan of data that has existed already before the function has been called (with some special exceptions like interior mutability). It's closer to behaving like a compile-time lock than a general-purpose pointer.

To return data by reference just return an owned Vec (without any borrowing), it holds data by reference already. Returning other newly-created data by reference is done with Box.

As a novice you will be tempted to overuse references, and it will make you fight the borrow checker, because they are a different feature than most people assume. Try not to use references for anything except function arguments.

1 Like