ADVERTISEMENT
ADVERTISEMENT

Rust program to print the length of string literal

Here, we are going to learn how to print the length of string literal in Rust programming language?
Submitted by Nidhi, on September 22, 2021

Problem Solution:

Here, we will create string literals using &str type. The value of string literal is known at compile-time, and we will print the length of the string literal using the string.len() function.

Program/Source Code:

The source code to print the length of string literal is given below. The given program is compiled and executed successfully.

// Rust program to print the length 
// of string literal

fn main() {
    let message:&str   ="Think big, Think beyond";  
    
    println!("Message: {}",message);
    println!("Message length: {}",message.len());
}

Output:

Message: Think big, Think beyond
Message length: 23

Explanation:

Here, we created a string literal message using &str type. Then we printed the values of the created string literal and its length 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.