Mental model for foo::::baz syntax?

⚓ Rust    📅 2026-05-02    👤 surdeus    👁️ 3      

surdeus

I've used other languages with generic types. I always thought of it as similar to a function call, passing arguments to an abstraction. So:

some_function(a, b)

Creates an "instance" (a call) of some_function by passing specific arguments a and b.

SomeType(a, b)

Creates an instance of the class/type SomeType, calling its constructor with arguments. Not all languages have this exact syntax, but some do (Kotlin, Swift, Python, C++). And lastly:

HashTable<Int, String>

Creates an instance of a type, by passing two arguments to a generic type. All three share this idea of applying/calling an abstraction with arguments. In the last, the () changes to <>, and the application there is a compile-time thing, not a runtime call.

I understand the problems with parsing that last one, because < and > are reused for comparison operators. Rust's syntax appears to be:

HashTable::<i32, String>

This confuses me because :: usually means to me that we're drilling down in a path of some kind. Should I still think of that as "the type HashTable<i32, String>"? So ::< ... > is simply how you "apply/call/instantiate" the generic type and pass it arguments. Or should I think of :: as some kind of path drilling?

3 posts - 3 participants

Read full topic

🏷️ Rust_feed