3
我想將任何數字類型的列表變成浮動列表。下面的代碼編譯失敗:從特質只適用於值,但我有參考
#[cfg(test)]
mod tests {
#[test]
fn test_int_to_float() {
use super::int_to_float;
assert_eq!(vec![0.0, 1.0, 2.0], int_to_float(&[0, 1, 2]));
}
}
pub fn int_to_float<I>(xs: I) -> Vec<f64>
where
I: IntoIterator,
f64: From<I::Item>,
{
xs.into_iter().map(f64::from).collect()
}
該錯誤消息是
error[E0277]: the trait bound `f64: std::convert::From<&{integer}>` is not satisfied
--> src/main.rs:6:41
|
6 | assert_eq!(vec![0.0, 1.0, 2.0], int_to_float(&[0, 1, 2]));
| ^^^^^^^^^^^^ the trait `std::convert::From<&{integer}>` is not implemented for `f64`
|
= help: the following implementations were found:
<f64 as std::convert::From<i8>>
<f64 as std::convert::From<i16>>
<f64 as std::convert::From<f32>>
<f64 as std::convert::From<u16>>
and 3 others
= note: required by `int_to_float`
我明白I::Item
是一個i32
(&i32
)的引用,但f64::from
僅對值來實現。我如何得到這個編譯?
我們收到很多格式不合理,過於龐大的帖子和難以回答的問題。我想認識到這個問題並不是那些,並且表現出很大的努力! – Shepmaster