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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With