英文:
Rust generic function accepting both primitive and rug integers
问题
use rug::Integer;
use std::ops::Mul;
fn mymul<T, S>(x: &T, y: &T) -> T
where
T: Clone + From<S> + Mul<Output = S>,
{
T::from(x.clone() * y.clone())
}
fn main() {
let x: u64 = 3847381;
let y: u64 = 28478195;
let rug_x = Integer::from(x);
let rug_y = Integer::from(y);
println!("{} == {:?}", mymul(&x, &y), mymul(&rug_x, &rug_y));
}
无法匹配的类型错误
--> src/main.rs:22:43
|
22 | println!("{} == {:?}", mymul(&x, &y), mymul(&rug_x, &rug_y));
| ^^^^^^^^^^^^^^^^^^^^^ one type is more general than the other
|
= note: expected struct `integer::arith::MulIncomplete<'a>`
found struct `integer::arith::MulIncomplete<'_>`
note: the lifetime requirement is introduced here
--> src/main.rs:12:31
|
12 | for<'a> &'a T: Mul<&'a T, Output = S>,
| ^^^^^^^^^^
英文:
I'm trying to write a generic function that operates on integers, which can accept either primitive integers or multiprecision ones (via rug
). The tricky bit is that references to rug Integer
s have lazy arithmetic operations, that return incomplete types until cast into an integer. So I thought something like the following would work, but I can't seem to get the lifetime setting correct (and rust-analyzer is not being super verbose in helping):
use rug::Integer;
use std::ops::Mul;
fn mymul<T, S>(x: &T, y: &T) -> T
where
T: 'static + From<S>,
for<'a> &'a T: Mul<&'a T, Output = S>,
{
T::from(x * y)
}
fn main() {
let x: u64 = 3847381;
let y: u64 = 28478195;
let rug_x = Integer::from(x);
let rug_y = Integer::from(y);
println!("{} == {:?}", mymul(&x, &y), mymul(&rug_x, &rug_y));
}
with error
error[E0308]: mismatched types
--> src/main.rs:22:43
|
22 | println!("{} == {:?}", mymul(&x, &y), mymul(&rug_x, &rug_y));
| ^^^^^^^^^^^^^^^^^^^^^ one type is more general than the other
|
= note: expected struct `integer::arith::MulIncomplete<'a>`
found struct `integer::arith::MulIncomplete<'_>`
note: the lifetime requirement is introduced here
--> src/main.rs:12:31
|
12 | for<'a> &'a T: Mul<&'a T, Output = S>,
| ^^^^^^^^^^
Any idea how to do this sort of thing correctly?
答案1
得分: 3
以下是翻译好的部分:
正确的使用HRTB指定此函数的方式是:
fn mymul<T>(x: &T, y: &T) -> T
where
for<'a> &'a T: Mul<&'a T>,
for<'a> <&'a T as Mul<&'a T>>::Output: Into<T>,
{
(x * y).into()
}
英文:
The right way to specify this function with HRTB is:
fn mymul<T>(x: &T, y: &T) -> T
where
for<'a> &'a T: Mul<&'a T>,
for<'a> <&'a T as Mul<&'a T>>::Output: Into<T>,
{
(x * y).into()
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论