Array
programming languages. In the program, sometimes we need to store a collection of data of the same type, like a list of student marks. Such type of collection is stored in a program using an Array.
Go
An array is a fixed-length sequence that is used to store homogeneous elements in the memory. Due to their fixed length array are not much popular like Slice in Go language. In Go language, an array is of value type not of reference type. So when the array is assigned to a new variable, then the changes made in the new variable do not affect the original array.
var myarr[3]string
// Elements are assigned using index
myarr[0] = "GFG"
myarr[1] = "GeeksforGeeks"
myarr[2] = "Geek"
Using shorthand declaration
arr:= [4]string{"geek", "gfg", "Geeks1231", "GeeksforGeeks"}
// by the number of elements present in it
// Using ellipsis
myarray:= [...]string{"GFG", "gfg", "geeks",
"GeeksforGeeks", "GEEK"}
Multi-Dimensional Array
Multi-Dimensional arrays are the arrays of arrays of the same type.
// Using shorthand declaration
// Here the (,) Comma is necessary
arr:= [3][3]string{{"C#", "C", "Python"},
{"Java", "Scala", "Perl"},
{"C++", "Go", "HTML"},}
Rust
An Array in Rust programming is a fixed-sized collection of elements denoted by [T; N] where is T is the element type and N is the compile-time constant size of the array.
We can create an array in 2 different ways:
- Simply a list with each element [a, b, c].
- Repeat expression [N, X]. This will create an array with N copies of X.
array[1] = 1;
array[2] = 2;
array[3] = 3;
array[4] = 4;
assert_eq!([1, 2 , 3 ,4], &array[1..]);
let arr = [1,2,3,4,5];
println!("array size is :{}",arr.len());
for index in 0..5 {
println!("index is: {} & value is : {}",index, arr[index]);
}
Multi Array
struct ArrayView<'arr> {
data: &'arr [[usize; 50]; 50],
offsets: [usize; 2],
size: [usize; 2],
}
Vector in Rust
Vector is a module in Rust that provides the container space to store values. It is a contiguous resizable array type, with heap-allocated contents.It can be increase size dynamically during runtime.Its length defines the number of elements present in the vector. Its capacity defines the actual allocated space on the heap of this vector that is in the form of 2^n.
let v : Vec<i64> = Vec::new();
or
let v = vec!['G','E','E','K','S'];
Examples
let index: usize = 3;
let ch: char = v[index];
let ch: Option<&char> = v.get(index);
//loop to iterate elements in vector
for i in v
{
// iterating through i on the the vector
print!("{} ",i);
}
v.push('A');
v.push('B');
v.push('C');
No comments:
Post a Comment