Blog: Think local, act local


#1

Wherein we look at some parts of Rust’s design that limit what we need to look at to understand a piece of code.


#2

I’d like to add one case where Rust is extremely non-local:

mod main {
    pub struct Foo;

    fn quux() {
        // It's impossible to use local reasoning
        // to find foo's implementation
        Foo.foo() 
    }
}

mod foo {
    mod bar {
        fn baz() {
            use main::Foo;
            impl Foo {
                pub fn foo(&self) {
                }
            }
        }                
    }
}

and one case that it’s more local then most mainstream OO languages: you can’t inherit data, so it’s relatively easy to understand what data can be reachable from a given method, because you need only to look at self’s fields.


#3

True, impls can be in arbitrary submodules. Still this particular case should be seen as an anti-pattern.