This is a good example for the Borrow
trait.
use std::borrow::Borrow;
struct Bar;
trait Foo {
fn hi(&self);
}
impl<B: Borrow<Bar>> Foo for B {
fn hi(&self) {
print!("hi")
}
}
fn main() {
let bar = Bar;
(&bar).hi();
&bar.hi();
}
More Related Contents:
- Traits and Rust
- How to get a reference to a concrete type from a trait object?
- Why can impl trait not be used to return multiple / conditional types?
- How do I implement a trait I don’t own for a type I don’t own?
- What makes something a “trait object”?
- Is it possible to use `impl Trait` as a function’s return type in a trait definition?
- How do I return an instance of a trait from a method?
- I implemented a trait for another trait but cannot call methods from both traits
- Why would I implement methods on a trait instead of as part of the trait?
- How does the mechanism behind the creation of boxed traits work?
- Trait implementation for both a trait object and for direct implementors of the trait
- Why is the `Sized` bound necessary in this trait?
- How can I implement Rust’s Copy trait?
- Is there any trait that specifies numeric functionality?
- Understanding Traits and Object Safety
- How do I pass a closure through raw pointers as an argument to a C function?
- Can’t implement a trait I don’t own for all types that implement a trait I do own
- How do I specify that a struct field must implement a trait?
- Rust equivalent to Swift’s extension methods to a protocol?
- Cannot use moved BufReader after for loop with bufreader.lines()
- How do I create a heterogeneous collection of objects?
- How do I stop iteration and return an error when Iterator::map returns a Result::Err?
- Are polymorphic variables allowed?
- Efficiently mutate a vector while also iterating over the same vector
- Why do I need rebinding/shadowing when I can have mutable variable binding?
- How can I put an async function into a map in Rust?
- How do I synchronously return a value calculated in an asynchronous Future?
- Removing elements from a Vec based on some condition [duplicate]
- What’s the difference between using the return statement and omitting the semicolon in Rust?
- How to initialize the logger for integration tests?