Q:

Rust program to create a simple structure

belongs to collection: Rust Structures Programs

0

In this program, we will create a structure with few members. Then we will create and initialize the structure with values.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program/Source Code:

The source code to create a simple structure is given below. The given program is compiled and executed successfully.

// Rust program to create a simple structure

struct MyStruct {
   num:u32,
   str:String
}

fn main() {

   let obj = MyStruct {
      num:100,
      str:String::from("Hello World")
   };
   
   println!("Structure elements");
   println!("  Num: {}",obj.num);
   println!("  Str: {}",obj.str);
}

Output:

Structure elements
  Num: 100
  Str: Hello World

Explanation:

In the above program, we created a structure MyStruct and function main(). The MyStruct structure contains two members num and str.

In the main() function, we created and initialized the structure. Then we access structure elements using the "." operator and printed them.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Rust program to create a structure with default va... >>