rust


How do I include the end value in a range?


I wanted to create a vector with 'a'..'z' values (inclusive).
This doesn't compile:
let vec: Vec<char> = ('a'..'z'+1).collect();
What's the idiomatic way to have 'a'..'z'?
As of Rust 1.17, you cannot. These go by the name "inclusive ranges", and they are not yet stable:
#![feature(inclusive_range_syntax)]
fn main() {
for i in 0...26 {
println!("{}", i);
}
}
Even then, you cannot iterate over a range of characters:
error[E0277]: the trait bound `char: std::iter::Step` is not satisfied
--> src/main.rs:4:5
|
4 | / for c in 'a'..'z' {
5 | | println!("{}", c);
6 | | }
| |_____^ the trait `std::iter::Step` is not implemented for `char`
|
= note: required because of the requirements on the impl of `std::iter::Iterator` for `std::ops::RangeInclusive<char>`
This is because the concept of "next" doesn't really make sense for the vast majority of characters. What would the successor of 🤓 or 🏳 be?
As a workaround, you can iterate over bytes and cast those back to chars:
for i in b'a'..(b'z' + 1) {
let c = i as char;
println!("{}", c);
}
Or you could specify the set of characters you are interested in:
static ALPHABET: &str = "abcdefghijklmnopqrstuvwxyz";
for c in ALPHABET.chars() {
println!("{}", c);
}

Related Links

How to execute Rust code directly on Unix systems? (using the shebang)
Convenient 'Option<Box<Any>>' access when success is assured?
Use a closure inside a thread
How to change the variable from inside Fn closure in Rust?
Create mutual reference for rust [duplicate]
How do I safely use an object while it is mutably borrowed?
Workspace giving two different behaviors in Rust
Restricting object lifetimes in Rust
Project with serde cannot compile
Cannot infer an appropriate lifetime for autoref due to conflicting requirements
How to define a hashmap with contents in 1 line in Rust? [duplicate]
How solve “cannot index a value of type `usize`” error?
Preserve lifetime of an argument in another object [duplicate]
How can I use multiple items in a Vec at a time in Rust?
Mutable reference to a tuple as input parameter
What is the difference between the two styles of documentation comments in Rust? (/// vs //!) [duplicate]

Categories

HOME
mc
yaml
command-prompt
tcp
mirc
iptables
openfoam
telegraf
responsive-design
erd
fbloginview
android-5.1.1-lollipop
jqxgrid
gravity
boomi
hdl
pymongo
substring
chronicle-queue
opentext
plyr
ipa
tortoisehg
acrobat
sfml
fatfs
credit-card
nsmutablearray
universal
counting
pdfminer
identify
ng-repeat
django-smart-selects
text-decorations
suds
morphline
ol3-google-maps
entropy
freshdesk
mobile-angular-ui
.when
yargs
ternary-operator
tablespace
nvda
django-testing
tsqlt
wolfram-language
yii2-model
android-async-http
swiffy
gridbaglayout
taco
servicestack-bsd
helm
android-mapview
blank-line
bjam
erlog
findersync
fasterxml
multivariate-testing
git-reset
senchatouch-2.4
pre
mod-perl
grails-2.3
san
mousemotionlistener
unicoins
ui-design
xmlwriter
handshake
yuidoc
data-dump
botnet
asp.net-mvc-views
android-input-method
asdoc
powergui
jquery-effects
sqlsitemapprovider
activex-exe
html-generation

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App