What happend when method name is duplicated?

pub trait FollowDBFormat {
    fn trunc(self, ) -> String;

impl FollowDBFormat for f64 {
    fn trunc(self, ) -> String {
        FDBF_REGEX.replace(format!("{:?}", self).as_str(), r"$1$2").to_string()


What happened new implemented trait has the same name as any of exisiting methods?
In the above case, FollowDBFormat has trunc() method whose name is already in f64.

The inherent method (not part of a trait) will take precedence. You can instead call FollowDBFormat::trunc(x) if it can be inferred that you mean the f64 version, e.g. by the type of the parameter, or you can be explicit with <f64 as FollowDBFormat>::trunc(x).

See also: Advanced Traits - The Rust Programming Language


Also, trait method will be used when the type it is called on is a generic parameter, i.e. if it's T: FollowDBFormat.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.