ADVERTISEMENT
ADVERTISEMENT

Rust program to create a simple structure

Rust | Structure Example: Write a program to create a simple structure.
Submitted by Nidhi, on October 27, 2021

Problem Solution:

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

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.

Rust Structures Programs »



ADVERTISEMENT




ADVERTISEMENT



Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.