Rust Programming
A Guide to Optimizing Print Output in Your Rust Programming
- Output :
- Print! Macro In Rust :
- println! Macro In Rust :
- format! Macro In Rust :
- Print Variables In Rust :
- Print Multiple Variables In Rust :
- Print Newline Character In Rust :
In Rust Program, We can use the print!
macro to print strings, numbers and variables the output on the screen.
For example:-
fn main() {
print!("Hello, World!");
}
¶Output :
Hello, World!
Above, print!
is a macro which prints the text inside double quotes.
In Rust Program, there are two alternatives of the print:
- print!()
- println!()
¶Print! Macro In Rust :
As above-mentioned earlier, the print! macro prints the text under double quotes. For example-
fn main() {
print!("Rust is fun! ");
print!("I like Rust programming.");
}
¶Output :
Rust is fun! I like Rust programming.
We can see that have used two print!
macros to print two several strings. However, both the strings are printed in the equivalent line.
To individual the print strings in several lines, we can use the println!
macro who will add a new metrical line character at the end.
¶println! Macro In Rust :
fn main() {
println!("Rust is fun!");
println!("I like Rust programming.");
}
¶Output :
Rust is fun!
I like Rust programming.
Above, we can see output is printed in two individual lines.
This is println!
adds a recent line character (enter) at the end, that way the second text is printed in the after line.
¶format!
Macro In Rust :
The format!
macro makes a formatted string by combining verbal text with formatted expressions. It doesn’t print anything to the pacify; it returns a String that we can print or use as needed.
¶Basic Usage:
fn main() {
let name = "Alice";
let age = 30;
// Create a formatted string
let formatted_string = format!("Name: {}, Age: {}", name, age);
// Print or use the formatted string
println!("{}", formatted_string);
}
¶Options of Formatting :
The format string may comprise placeholders for evolution. This placeholders start with {}
and include optional formatting parameters:
fn main() {
let pi = 3.14159;
// Formatting with precision
let formatted_float = format!("Pi with precision: {:.2}", pi);
println!("{}", formatted_float);
}
We can also use both format!
and println!
support various formatting options for different types. For example:
fn main() {
let number = 42;
// Formatting as hexadecimal
println!("Number in hexadecimal: {:x}", number);
}
this is just basic examples, and you can discovery more detailed information in the official Rust documentation on formatting.
¶Print Variables In Rust :
We can conduct the same print! and println! macros to print variables in Rust Program. For example-
fn main() {
let age = 21;
// print the variable using println!
println!("{}", age);
// print the variable using print!
print!("{}", age);
}
¶Output :
21
21
In the upon example advertising the print statements:
print!("{}", age);
println!("{}", age);
See, {}
is a placeholder replaced by the value of the variable below the comma. So, we get 21 as output instead of {}
.
You, can further add text with the placeholder to format your output. For example-
fn main() {
let age = 21;
// print the variable using println!
println!("Age = {}", age);
}
¶Output :
Age = 21
So, you can see the output glory more informatory.
¶Print Multiple Variables In Rust :
We can exercise a single println!
macro to print multiple variables to-gether.
For example-
fn main() {
let age = 21;
let name = "Azad";
// print the variables using println!
println!("Name = {}, Age = {}", name, age);
}
¶Output :
Name = Azad, Age = 21
Above, we can see variables are printed in sequence. the first variable name make change the first placeholder and the second variable age make change the second placeholder.
Still, we can also mark the numbering for placeholders to print variables in other order. For example-
fn main() {
let age = 21;
let name = "Azad";
// print the variable using println!
println!("Name = {0}, Age = {1}", name, age);
}
¶Output :
Name = Azad, Age = 21
Below, the placeholder:-
-
{0}
is changed by the first variablename
-
{1}
is changed by the second variableage
Likewise, you can also use the variable names straight away inside the placeholder. For example-
fn main() {
let age = 21;
let name = "Azad";
// print the variables using println!
println!("Name = {name}, Age = {age}");
}
¶Output :
Name = Azad, Age = 21
Below, instead of using variables individually after comma, you have directly shifted them inside the placeholder.
-
{name}
- prints value of thename
variable -
{age}
- prints value of theage
variable
¶Print Newline Character In Rust :
In Rust program, we can print a newline character using the println!
macro or the print!
macro towards with the "\n"
escape sequence.
Here’s an example:
fn main() {
// Using println! macro to print with a newline
println!("Hello, world!");
// Using print! macro with the "\n" escape sequence
print!("This is a line without a newline");
print!("\n"); // Adding a newline separately
// Another way using println! with an empty format string
println!();
// You can also use the write! macro from the std::fmt module
use std::fmt::Write;
let mut buffer = String::new();
write!(&mut buffer, "This is written to a string without a newline").unwrap();
writeln!(&mut buffer).unwrap(); // Adding a newline separately
println!("{}", buffer);
}
¶Output :
Hello, world!
This is a line without a newline
This is written to a string without a newline
A Comprehensive Guide to Effective Comments In Rust Programming
A Guide To Variables and Mutability In Rust Programming
All Tutorials in this playlist
Popular Tutorials
Categories
-
Artificial Intelligence (AI)
11
-
Bash Scripting
1
-
Bootstrap CSS
0
-
C Programming
14
-
C#
0
-
ChatGPT
1
-
Code Editor
2
-
Computer Engineering
3
-
CSS
28
-
Data Structure and Algorithm
18
-
Design Pattern in PHP
2
-
Design Patterns - Clean Code
1
-
E-Book
1
-
Git Commands
1
-
HTML
19
-
Interview Prepration
2
-
Java Programming
0
-
JavaScript
12
-
Laravel PHP Framework
37
-
Mysql
1
-
Node JS
1
-
Online Business
0
-
PHP
28
-
Programming
8
-
Python
12
-
React Js
19
-
React Native
1
-
Redux
2
-
Rust Programming
15
-
SEO - Search Engine Optimization
1
-
Tailwind CSS
1
-
Typescript
10
-
Uncategorized
0
-
Vue JS
1
-
Windows Operating system
1
-
Woocommerce
1
-
WordPress Development
2
Tags
- Artificial Intelligence (AI)
- Bash Scripting
- Business
- C
- C Programming
- C-sharp programming
- C++
- Code Editor
- Computer Engineering
- CSS
- Data Structure and Algorithm
- Database
- Design pattern
- Express JS
- git
- Git Commands
- github
- HTML
- Java
- JavaScript
- Laravel
- Mathematics
- MongoDB
- Mysql
- Node JS
- PHP
- Programming
- Python
- React Js
- Redux
- Rust Programming Language
- SEO
- TypeScript
- Vue JS
- Windows terminal
- Woocommerce
- WordPress
- WordPress Plugin Development