Q:

Rust program to print the length of string literal

belongs to collection: Rust Basic Programs

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Rust Basic Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Rust program to read an integer number from the us... >>
<< Rust program to create string literals...