How to call a method when a trait and struct use the same method name?

To specify which method to call, whether inherent or provided from a trait, you want to use the fully qualified syntax:

Type::function(maybe_self, needed_arguments, more_arguments)
Trait::function(maybe_self, needed_arguments, more_arguments)

Your case doesn’t work because Vec doesn’t have a method called get! get is provided from the Deref implementation to [T].

The easiest fix is to call as_slice directly:

self.as_slice().get(index).map(|v| v as &Any)

You could also use the fully qualified syntax which requires the angle brackets in this case (<...>) to avoid ambiguity with declaring an array literal:

<[i32]>::get(self, index).map(|v| v as &Any)

universal call syntax

Note that while Rust originally used the term universal function call syntax (UFCS), the usage of this term conflicted with the existing understood programming term, so the use of it is not suggested. The replacement term is fully qualified syntax.

Leave a Comment