Sunday , May 29 2022
Home / Rust / Control Flow in Rust

Control Flow in Rust

In this article, you can find out how the control flow works in Rust Lang.

A Tuple is a general way of grouping together some number of other values with a variety of types into one compound type. Tuples have a fixed length: once declared, they cannot grow or shrink in size.

Another way to have a collection of multiple values is with an array. Every element of an array must have the same type. Arrays in Rust are different from arrays in some other languages because arrays in Rust have a fixed length, like tuples.

fn main() {
    // simple tuple print
    let mixData = (43,54.4,"Hello World");

    // tuple with type restriction
    let mixData: (i32,f32,bool,String) = (43,54.65,true,"Hello World".to_string()); 

    // tuple with variable
    let x = 32;
    let y = 43.54;
    let z = "Hello World";
    let mixData = (x,y,z);

    //simple array creation
    let sameData = [1,2,3,4,5,6];
    println!("{:?}", sameData);

    //array with index calling
    let mut sameData = [10;6]; //Element is 6 means you have 0-5 index
    sameData[0] = 4;
    sameData[1] = 5;
    sameData[2] = 7;
    sameData[3] = 110;
    sameData[4] = 6;
    sameData[5] = 19;
    println!("{:?}", sameData);

    //array with tuple mixing
    let sameData = [("Faheem",23,5.8,true),("Ibad",38,5.4,false),("Salar",29,6.8,true)];
    println!("{:?}", sameData[0].0);

    // array access with for loop
    let sameData = [10,20,30,40,50];
    for x in (0..=4)
        println!("{}", sameData[x]);

    // array access with for loop using iter()
    let sameData = [10,20,30,40,50];
    for x in sameData.iter()
        println!("{}", x);

    // array access with while loop
    let sameData = [10,20,30,40,50];
    let mut count = 0;
    while count < 5{
        println!("{}", sameData[count]);
        count += 1;

The above mention code shows how you can use the different control structure in Rust Lang.

In the below mention video you can see each an every step to do this:

About Faheem

An adaptable and responsible Software Engineer, who enjoys working with computers and the opportunity to learn new skills and work with new technologies is particularly attractive to me.

Check Also

Linux Installation on Computer in Hindi/Urdu

In this article, you can find the way you can install Linux on your own …

One comment

  1. NIce Sir. Very Helpful. Keep Doing This.

Leave a Reply

Your email address will not be published.