Content deleted Content added
→Range as a data type: add UML class diagram |
No edit summary |
||
Line 5:
# An alternative to [[iterator]].
== Range of a variable ==
The range of a variable is given as the set of possible values that that variable can hold. In the case of an integer, the variable definition is restricted to whole numbers only, and the range will cover every number within its range (including the maximum and minimum). For example, the range of a [[signedness|signed]] [[16-bit]] [[Integer (computer science)|integer]] variable is all the integers from −32,768 to +32,767.
== Range of an array ==
{{Main|Array data type#Indexing notation}}
Line 45:
</syntaxhighlight>
Example in [[Python (programming language)|Python]].
Rust have a built-in range struct in the standard library in {{Mono|std::ops::Range}}.<ref>{{cite web |title=Range in std::ops - Rust |url=https://doc.rust-lang.org/std/ops/struct.Range.html |website=doc.rust-lang.org |access-date=17 October 2024}}</ref>▼
<syntaxhighlight lang="python">
from dataclasses import dataclass
@dataclass
==See also==▼
class Range[T]:
*[[Interval (mathematics)|Interval]]▼
start: T
end: T
</syntaxhighlight>
▲[[Rust (programming language)|Rust]] have a built-in range struct in the standard library in {{Mono|std::ops::Range}}.<ref>{{cite web |title=Range in std::ops - Rust |url=https://doc.rust-lang.org/std/ops/struct.Range.html |website=doc.rust-lang.org |access-date=17 October 2024}}</ref>
== Range as a operator ==
[[Rust (programming language)|Rust]] have the {{Mono|..}} and {{Mono|..{{=}}}} operators.
<syntaxhighlight lang="rust">
let heartwarming = "heartwarming!".to_string();
let warm = &heartwarming[5..9];
</syntaxhighlight>
[[Zig (programming language)|Zig]] also have the {{Mono|..}} operator.
<syntaxhighlight lang="zig">
// To iterate over consecutive integers, use the range syntax.
var sum: usize = 0;
for (0..5) |i| {
sum += i;
}
</syntaxhighlight>
▲== See also ==
▲* [[Interval (mathematics)|Interval]]
== References ==
Line 56 ⟶ 82:
[[Category:Articles with example C Sharp code]]
[[Category:Articles with example PHP code]]
[[Category:Articles with example Python (programming language) code]]
[[Category:Articles with example Rust code]]
[[Category:Arrays]]
[[Category:Programming constructs]]
|