Arrays: Difference between revisions

886 bytes added ,  9 years ago
→‎{{header|Rust}}: Initial version.
No edit summary
(→‎{{header|Rust}}: Initial version.)
Line 3,872:
numArray(1,1) = 987.2
print chrArray$(1,1);" ";numArray(1,1)</lang>
 
=={{header|Rust}}==
 
The Rust book has a [http://doc.rust-lang.org/1.0.0-beta/book/arrays-vectors-and-slices.html tutorial on arrays].
 
By default, arrays are immutable unless defined otherwise.
 
<lang rust>let a = [1, 2, 3]; // immutable array
let mut m = [1, 2, 3]; // mutable array
let zeroes = [0; 200]; // creates an array of 200 zeroes</lang>
 
To get the length and iterate,
 
<lang rust>let a = [1, 2, 3];
a.len();
for e in a.iter() {
e;
}</lang>
 
Accessing a particular element uses subscript notation, starting from 0.
 
<lang rust>let names = ["Graydon", "Brian", "Niko"];
names[1]; // second element</lang>
 
Dynamic arrays in Rust are called vectors.
 
<lang rust>let v = vec![1, 2, 3];</lang>
 
However, this defines an immutable vector. To add elements to a vector, we need to define v to be mutable.
 
<lang rust>let mut v = vec![1, 2, 3];
v.push(4);
v.len(); // 4</lang>
 
=={{header|Sather}}==
Anonymous user