String::borrow vs String::as_str, is there extra cost for borrow?

String::borrow vs String::as_str both would give &str. I would like to use borrow() for possibility to later refactor structures to Cow<str>, but would that incur any extra costs due to .borrow() being a trait method?

I guess compiler might optimise that as it is a trait on a predefined type.

Thank you

No, when the compiler knows the underlying type, trait methods have the same cost as normal methods.


To be clear: this means that in pretty much all cases except for dyn Trait, static dispatch is used. (For dyn Trait, a vtable is built and indirect calls are made, usually.)

So, unless you specifically request dynamism, traits remain a purely compile-time construct.