• PHP
  • Spring
  • SQL Tutorial
  • Core Java
  • Struts
  • Java IO
  • JSP
  • Servlet
  • JavaScript

  • <

    Rust Array

    Array is a collection of same data types.Arrays are static. This means that an array once initialized cannot be resized.Each memory block represents an array element.

    Rust Array Declaring and Initializing

    syntax for declare and initialize arrays
    let variable_name = [value1,value2,value3];
    
    
    let variable_name:[dataType;size] = [value1,value2,value3];
    
    
    let variable_name:[dataType;size] = [default_value_for_elements,size];
    

    Mutable array Example

    mut keyword can be used to declare a mutable array.mutable array is used to modify array elements.

    fn main(){
       let mut arr:[i32;4] = [1,2,3,4];
       arr[1] = 0;
       println!("{:?}",arr);
    }
    

    Output:
    [1, 0, 3, 4]
    

    capture

    Array without data type


    fn main(){
       let arr = [1,2,3,4];
       println!("array is {:?}",arr);
       println!("size of array is :{}",arr.len());
    }
    

    : Output
    
    array is [1, 2, 3, 4]
    size of array is :4
    

    Rust Array Constants Example

    const keyword is used to define constant.


    fn main() {
       const N: usize = 20; 
       // pointer sized
       let arr = [0; N];
    
       print!("{}",arr[10])
    }
    

    capture

















    © copyright 2017-2021 Completedone pvt ltd.