Range Bounds Half Open And Inclusive
Half-open .. ranges compose with len() while ..= keeps the end bound; ranges are values you can test with contains, is_empty, and rev.
Code
fn main() {
// half-open: excludes the end, so it composes with len()
let letters = ['a', 'b', 'c', 'd', 'e'];
for i in 0..letters.len() {
print!("{} ", letters[i]);
}
println!();
// ..= is the inclusive form, so 'a'..='e' covers five chars
let mut inclusive = String::new();
for ch in 'a'..='e' {
inclusive.push(ch);
}
println!("inclusive: {inclusive}");
// ranges are values: contains and is_empty test bounds without looping
let working = 18..=65;
println!("contains 30: {}", working.contains(&30));
println!("contains 70: {}", working.contains(&70));
// rev walks the same bounds backwards; start > end is empty, not an error
let countdown: Vec<i32> = (1..=3).rev().collect();
println!("rev: {countdown:?}");
println!("5..1 is_empty: {}", (5..1).is_empty());
}How the snippet works
Range Bounds Half Open And Inclusive is built around the function main.
Iteration happens inside for i in 0..letters.len() — everything outside it runs once.
Watch out for
- Copy, run: the standard library answers every call in the file.
Using it
The main signature reads fn main()
The run emits 5 labeled sections — inclusive:, contains 30:, contains 70:, rev:, 5..1 is_empty: — one per step.
Key statements worth quoting
let countdown: Vec<i32> = (1..=3).rev().collect();
Interactive Tools for This Topic
SemVer Range CheckerCheck whether a semantic version satisfies an npm-style range such as caret, tilde, comparison sets, wildcards, or hyphen ranges.SPDX License LookupSearch common SPDX license identifiers and see permissions, conditions, limitations, copyleft strength, and OSI or FSF approval status.SRI Hash GeneratorGenerate Subresource Integrity (SRI) hashes from resource content using SHA-256, SHA-384, or SHA-512, and copy a ready-to-use script tag.
FAQ
- What does Range Bounds Half Open And Inclusive print when run?
- The script runs standalone; its 5 labeled outputs include `inclusive:`, `contains 30:`, `contains 70:`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.