In Rust, what is the idiomatic equivalent of Haskell's [n..m]? -


how produce list containing integers in rust? i'm looking equivalent of haskell's [n..m] or python's range(n, m+1) can't find anything.

i'm aware of int::range function , thought looking for, made iterate on range, not produce it.

note answer pertains pre-1.0 version of rust , not apply 1.0. specifically, std::iter::range , std::iter::range_inclusive removed.

as of rust 1.0.0-alpha, easiest way accomplish use convenience functions provided in module std::iter: range , range_inclusive, return iterators generating list of numbers in range [low, high) or [low, high], respectively.

in addition, can build vector iterator using collect method:

use std::iter::range_inclusive; let first_hundred: vec<i32> = range_inclusive(1, 100).collect(); println!("upper bound inclusive: {:?}, exclusive: {:?}",          first_hundred,          range(101, 201).collect::<vec<_>>()); 

note return value of collect has type explicitly specified in both uses above. normally, rust compiler can infer types of expressions without explicit specification, collect 1 of common cases type cannot inferred, in case because can't infer concrete type implements trait fromiterator<a>, return type of collect.

the type of generic return value can specified either explicit type in let definition statement or inline using function::<type>() syntax. since inference fails due not knowing concrete type implementing fromiterator<a>, it's possible, when explicitly specifying generic type, leave "holes" type arguments inferred, signified _. done second call collect above—in expression vec<_>, it's explicitly specified container receiving elements collect vec<t>, compiler figures out exact type t must be. currently, integers types left unspecified , can't inferred fall i32 (32-bit machine integer) default.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -