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?
Last Updated : September 22, 2021

Problem Statement

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 »



Related Programs

Advertisement
Advertisement


Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.