Home » Rust Struct Update Syntax

Rust Struct Update Syntax

by Online Tutorials Library

Update Syntax

Creating a new instance from other instances using Struct update syntax.

When a new instance uses most of the values of an old instance, then we can use the struct update syntax. Consider two employees employee1 and employee2.

  • First, create the instance employee1 of Employee structure:
  • Second, create the instance the employee2. Some values of the employee2 instance are the same as employee1. There are two ways of declaring the employee2 instance.

The first way is declaring the employee2 instance without syntax update.

The second way is declaring the employee2 instance by using syntax update.

The syntax ‘..’ specifies that the rest of the fields are not explicitly set and they have the same value as the fields in the given instance.

Let’s see a simple example of Structure:

Output:

Area of a right angled triangle is 300  

In the above example, the structure of a triangle is created, and it contains two variables, i.e., base and height of a right-angled triangle. The instance of a Triangle is created inside the main() method.


Next TopicMethod syntax

You may also like