简体   繁体   中英

Can I alias fully qualified syntax?

I have some code where I have many instances of fully qualified syntax; as an example:

mod hal {
    pub trait Backend {
        type Device;
    }
}

mod back {
    pub struct Backend {}

    impl ::hal::Backend for Backend {
        type Device = i32;
    }
}

fn main() {
    let d: back::Backend::Device = 0;
}

playground

In order to avoid errors like:

error[E0223]: ambiguous associated type
  --> src/main.rs:16:12
   |
16 |     let d: back::Backend::Device = 0;
   |            ^^^^^^^^^^^^^^^^^^^^^ ambiguous associated type
   |
   = note: specify the type using the syntax `<back::Backend as Trait>::Device`

Is there a nice way in which I can alias SomeType as SomeTrait ? Then, wherever this instance of fully qualified syntax is needed, I can write:

<S>::associated_fn(...)

Note that this error does not occur because there are actually multiple implementations of some trait's definition (which is what FQS is supposed to handle, according to The Rust Programming Language ).

In your example, you can rename some of the parts so you can refer to them without conflicts in the shortened form:

use hal::Backend;
use back::Backend as BackendImpl;

fn main() {
    let d: <BackendImpl as Backend>::Device = 0;
}

You may also consider defining a type alias, which is less ambiguous to access:

mod hal {
    pub trait Backend {
        type Device;
    }
}

mod back {
    pub struct Backend {}
    pub type Device = i32;
    impl ::hal::Backend for Backend {
        type Device = Device;
    }
}

fn main() {
    let d: back::Device = 0;
}

No, there is no way to alias the fully-qualified syntax. Doing so doesn't make sense to me as the whole point of this syntax is to be completely unambiguous.


All of this assumes that you actually need fully-qualified syntax. As the name indicates, there's usually shorter ways of writing the code. These are all equivalent if no other traits defining to_string are in scope and the type itself doesn't implement a method of the same name:

<i32 as ToString>::to_string(&42);
ToString::to_string(&42);
i32::to_string(&42);
42.to_string();

See also:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM