Rationale for choosing double colon

I curious what the rationale was for choosing to use :: for the namespace separator and for calling static methods. Was that copied from another programming language? It seems like the designers of Rust could have chosen to use a single colon or even a period since the compiler might be able to know from the left side that a static method is being invoked.

It's the namespace separator in C++. It needs to be distinct from a single colon, because that means type ascription, while a single dot would be ambiguous in some cases, e.g. unit structs:

struct Foo;

impl Foo {
    // type method
    fn bar() {
    }

    // instance method
    fn bar(self) {
    }
}

Foo.bar(); // which one is this?
4 Likes

Rust doesn't allow those bar definitions to coexist.

1 Like

While I didn't realize that, using the same symbol would still be an inferior choice from the point of view of human readers.

(But then again, there isn't a single perfect rationale for syntactic choices; AFAICT :: is first and foremost an artefact of history.)

2 Likes

This is kind of backwards, by the way. In Rust types and values have different namespaces, so distinguishing between . and :: tells the compiler which namespace to look the left side up in. This is useful because it means that type analysis can be done without any knowledge of the runtime behavior of the code. In a language like Python, types and values are really the same "kind" of thing, so Python only has . because there is no type analysis that would make :: meaningfully different.

3 Likes

Here's an example that's ambiguous if you use . for both types and values: Rust Playground

3 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.