Is there suggar for AsRef?
I'm frequently declaring string-like parameters with as ref to make it easier to interop with various incantations of strings (String, &str, &String, etc.) as so:
fn Fun<S: AsRef<str>>(my: S) { ... my.as_ref() ...}
However now reading back those implementations after a while, it's visually taking more real estate in the code than I feel it should, is there a way to shrink usages? For instance not having to declare a type param explicitly, not having to call as_ref() explicitly, etc.
Looking at this cheatsheet , the book , and the doc does not suggest that there is.
Best.
You could declare a trait alias (requires nightly) and/or use impl trait
trait AsStr = AsRef<str>;
fn my_func(arg: impl AsStr) {
arg.as_ref() ...
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.