Working with strings is a common requirement in real-world applications. Sometimes, you want a function that can take either a String
or a &str
as input without needing separate implementations. Rust's AsRef
trait provides a simple way to borrow data as a reference in a generic manner, enabling functions to work with various types that can be represented as a reference to a common type.
The AsRef
trait is a powerful standard library trait used for cheap conversions to a reference of another type. For example, it can be used to create a function that works with both owned (String
) and borrowed (&str
) string types. This approach is lightweight and avoids unnecessary allocations.
Implement a function print_message
that accepts any type that implements the AsRef<str>
trait. This function should:
AsRef<str>
.&str
) and print it to the console using println!
.AsRef
trait provides a .as_ref()
method to borrow a reference.String
and &str
implement AsRef<str>
, so they can be used interchangeably with this trait.