Hello, everyone.
As the title says, I'm quite curious about the order in which Rust finds associated functions and constants.
For example, in the case of the code below, Rust will look for inherent associated functions and constants first, then go over traits.
I read document about it from here.
But I'm not sure the document says associated functions and constants as well.
So, can anyone teach me about the order?
Example of impl detection
pub struct ImplDetector<T>(std::marker::PhantomData<T>);
pub trait EqualType<T> {
const IS_EQUAL_TYPE: bool = false;
fn is_equal_type() -> bool { false }
}
impl<T> EqualType<T> for ImplDetector<T> {}
impl<T> ImplDetector<(T, T)> {
pub const IS_EQUAL_TYPE: bool = true;
pub fn is_equal_type() -> bool { true }
}
fn main() {
struct A;
struct B;
assert!(ImplDetector::<(A, A)>::is_equal_type());
assert!(!ImplDetector::<(A, B)>::is_equal_type());
const _: () = {
// Why does it work?
assert!(ImplDetector::<(A, A)>::IS_EQUAL_TYPE);
assert!(!ImplDetector::<(A, B)>::IS_EQUAL_TYPE);
};
}