Remove leading '&'-reference from generic type
⚓ Rust 📅 2025-12-10 👤 surdeus 👁️ 4Is there a way to remove the leading reference on a generic so something like this is possible? The goal is to take either &T or T but still constrain on T::from and call T::from (not &T::From).
fn foo<T>(new: T) -> <T as std::ops::Mul<T>>::Output
where
T: std::ops::Mul<T> + Copy + From<i32>
{
new * T::from(100)
}
fn main() {
println!("{}", foo(&100));
}
error[E0277]: the trait bound `&{integer}: From<i32>` is not satisfied
--> src/main.rs:11:22
|
11 | println!("{}", foo(&100));
| --- ^^^^ the trait `From<i32>` is not implemented for `&{integer}`
| |
| required by a bound introduced by this call
|
note: required by a bound in `foo`
--> src/main.rs:5:34
|
3 | pub fn foo<T>(new: T) -> <T as std::ops::Mul<T>>::Output
| --- required by a bound in this function
4 | where
5 | T: std::ops::Mul<T> + Copy + From<i32>
| ^^^^^^^^^ required by this bound in `foo`
help: consider removing the leading `&`-reference
4 posts - 3 participants
🏷️ Rust_feed