I do not understand how I'm supposed to use a nightly only trait in my code (using stable toolchain) like std is doing:
use std::str::FromStr;
fn values<T>(start: &str, end: &str) -> Vec<T>
where
T: FromStr,
<T as FromStr>::Err: std::fmt::Debug,
{
// collection to accumulate values in
let mut values: Vec<_> = Vec::new();
// retrieve 'start' / 'end' of range
let start = start.parse().unwrap();
let end = end.parse().unwrap();
// method 1
for value in start..end {
values.push(value)
}
//method 2
let range = start..end;
values.extend(range);
//return
values
}
Both method returns the same error:
error[E0277]: the trait bound `T: Step` is not satisfied
--> src/lib.rs:22:12
|
22 | values.extend(range);
| ^^^^^^ the trait `Step` is not implemented for `T`
|
= note: required because of the requirements on the impl of `Iterator` for `std::ops::Range<T>`
help: consider further restricting this bound
|
5 | T: FromStr + Step,
| ^^^^^^
error[E0277]: the trait bound `T: Step` is not satisfied
--> src/lib.rs:16:18
|
16 | for value in start..end {
| ^^^^^^^^^^ the trait `Step` is not implemented for `T`
|
= note: required because of the requirements on the impl of `Iterator` for `std::ops::Range<T>`
= note: required because of the requirements on the impl of `IntoIterator` for `std::ops::Range<T>`
= note: required by `into_iter`
help: consider further restricting this bound
|
5 | T: FromStr + Step,
| ^^^^^^
The main problem is that Step
is only available in nightly: documentation
Turning my function signature to:
fn values<T>(start: &str, end: &str) -> Vec<T>
where
T: FromStr + Step,
<T as FromStr>::Err: std::fmt::Debug,
{
/* ... */
}
leads to following error on stable toolchain:
error[E0658]: use of unstable library feature 'step_trait': recently redesigned
--> src/lib.rs:6:18
|
6 | T: FromStr + Step,
| ^^^^
|
= note: see issue #42168 <https://github.com/rust-lang/rust/issues/42168> for more information
Standard library is perfectly able to make Range
stable even if its Iterator implementation needs nightly only Step
trait.
fn main() {
// Here the range 0..11 is turned into an iterator requiring Step trait
// Step trait is implemented for u8
// Compilation without any error on stable toolchain
for i in 0u8..11 {
println!("{}", i);
}
}
How can I write my generic values
function for all primitive integers on stable toolchain? (all implements Step
trait)