Structs are one of Rust's fundamental building blocks for creating custom types. They allow you to group together related data under one type, making your programs easier to understand and maintain. In this challenge, you'll work with Rust structs by creating a simple structure to represent a Person
.
A struct in Rust can have named fields with different data types. For example:
Once defined, you can create instances of the struct and access or modify its fields using dot notation.
Your task is to define a struct called Person
with the following fields:
name
: A String
representing the person's name.age
: A u8
representing the person's age.You must also implement a function is_adult
that takes a reference to a Person
and returns true
if the person's age is 18 or older, and false
otherwise.
Person
with the fields name
and age
.is_adult
to determine if a person is an adult (18 or older).Here's how your code will be tested:
If you're having trouble, consider these hints:
&
to pass references to avoid unnecessary copies.age
field directly in a comparison for the is_adult
function.