summaryrefslogtreecommitdiff
path: root/rust/arrays.rs
diff options
context:
space:
mode:
Diffstat (limited to 'rust/arrays.rs')
-rw-r--r--rust/arrays.rs40
1 files changed, 40 insertions, 0 deletions
diff --git a/rust/arrays.rs b/rust/arrays.rs
new file mode 100644
index 0000000..2e22751
--- /dev/null
+++ b/rust/arrays.rs
@@ -0,0 +1,40 @@
+// ARRAYS - similar than C
+//
+//array size must be known at compile time
+//
+// signature: [T; length]
+// let var: [T; length] = foo
+
+fn main() {
+ let arr: [i32; 5] = [1, 2, 3, 4, 5];
+
+ // Implicit declaration:
+ let myarr = ['a', 'b', 'c'];
+ println!("{}", arr.len());
+
+ // Initialize all elements: [value, num of elements]
+ let auto_arr: [i32; 100] = [1; 100];
+
+ for c in arr {
+ print!("{} ", c);
+ }
+
+ for c in myarr {
+ print!("{} ", c);
+ }
+ println!("");
+
+ println!("array sizes: arr {} myarr {}", std::mem::size_of_val(&arr),
+ std::mem::size_of_val(&myarr));
+
+ // Accessing specific elements in an array can be done via indexing or .get method
+ // .get method returns an Option<T>, so we also need to use unwrap:
+
+ let many: [i32; 10] = [1, 3, 4, 6, 8, 34, 60, 44, 94, 10];
+
+ // great, either many[3] or &many[3] works, shrug
+ println!("Via index: {} - Via get method: {}",
+ &many[3], many.get(6).unwrap());
+}
+
+