1
我正在嘗試編寫一個類似於內置Range
的Rust函數,但我希望返回只有X個數字的東西,並將其作爲列表返回,就是爲什麼我試圖做這個功能: extern crate num;無法創建使用文字零的通用函數
use num::Integer;
fn positions<T: Integer>(start: T, step: T, len: T) -> Vec<T> {
(0..len).map(|i| start + step * i).collect()
}
fn main() {
println!("{:?}", positions(10, 2, 10));
}
除非我得到一個編譯器錯誤:
error[E0308]: mismatched types
--> src/main.rs:6:9
|
6 | (0..len).map(|i| start + step * i).collect()
| ^^^ expected integral variable, found type parameter
|
= note: expected type `{integer}`
found type `T`
= help: here are some functions which might fulfill your needs:
- .div_floor(...)
- .gcd(...)
- .lcm(...)
- .mod_floor(...)
error[E0308]: mismatched types
--> src/main.rs:6:37
|
6 | (0..len).map(|i| start + step * i).collect()
| ^expected type parameter, found integral variable
|
= note: expected type `T`
found type `{integer}`