Hello, World!
Easy
+2 pts
🎯 In Python, "Hello, World!" is just a str — there's only one string type and you rarely think about it.
Rust has two main string types, and understanding the difference early will save you a lot of confusion:
&str(string slice) — a borrowed reference to string data. Think of it like a read-only view. String literals like"hello"are&strby default.String(owned string) — a heap-allocated, growable string that you own and can modify. Closer to Python'sstrin terms of what you can do with it.
The function signature here asks you to return a String, not a &str. That means you need to convert the string literal into an owned String.
Common ways to do this:
"hello".to_string() // calls the ToString trait
String::from("hello") // explicit construction
"hello".into() // uses the Into trait (type must be inferrable)
All three are equivalent — pick whichever reads best to you. You'll see .to_string() and String::from() most often in Rust codebases.
Your Task
Implement the hello_world function so it returns "Hello, World!" as an owned String.
Example
assert_eq!(hello_world(), "Hello, World!");
Further Reading
- The Rust Book — What Is Ownership? — foundational for understanding
Stringvs&str - The Rust Book — The String Type — deep dive into how Rust strings work under the hood
fn hello_world() -> String {
// add your code here to return the string "Hello, World!"
todo!()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_hello_world() {
assert_eq!(hello_world(), "Hello, World!");
}
}
Topics