ADVERTISEMENT
ADVERTISEMENT

Rust program to create mutable variables

Here, we are going to learn how to create mutable variables in Rust programming language?
Submitted by Nidhi, on September 19, 2021

Problem Solution:

Here, we will create mutable variables using the mut keyword. By default Rust variables are immutable, we cannot change their values.

Program/Source Code:

The source code to create mutable variables is given below. The given program is compiled and executed successfully.

// Rust program to create mutable variables

fn main() {
	let mut var1=10;        //32-bit signed integer 
	let mut var2=30.12;     //32-bit floating point number
	let mut var3=true;      //Boolean value
	let mut var4='A';       //Character

	var1 = 20;
	var2 = 30.24;
	var3 = false;
	var4 = 'B';

	println!("Var1: {}",var1);
	println!("var2: {}",var2);
	println!("var3: {}",var3);
	println!("Var4: {}",var4);
}

Output:

Var1: 20
var2: 30.24
var3: false
Var4: B

Explanation:

In the main() function, we created 4 mutable variables using the mut keyword. Then we printed the value of variables using println!() macro on the console screen.

Rust Basic Programs »



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


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.