How to properly create a struct in Rust with String fields?
Hi everyone! I just started learning Rust and encountered a problem when creating a simple struct that should contain string fields. In other languages (like Python or Java), I would just declare a string field and it would work. But in Rust, it's somehow more complicated.
Here's what I'm trying to do:
```rust
struct Person {
name: &str,
age: u32,
}
fn main() {
let person = Person {
name: "Alice",
age: 30,
};
println!("{} is {} years old", person.name, person.age);
}
```
The compiler gives an error: `missing lifetime specifier`. I understand that this is because `&str` is a reference, and the compiler doesn't know how long it will live. But I don't want to bother with lifetimes, I just want the struct to own its data.
I tried
No answers yet.
Log in to leave an answer