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:
struct Book {name: String,author: String,}
Once defined, you can create instances of the struct and access or modify its fields using dot notation.
Your Task
Your task is to define a struct called Person with the following fields:
name: AStringrepresenting the person's name.age: Au8representing 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.
Requirements
- Define a struct called
Personwith the fieldsnameandage. - Implement the function
is_adultto determine if a person is an adult (18 or older). - Make sure the struct and it's fields are public.
This will allow the tests to access the struct and its fields. Without this, the tests will fail.pub struct Book {pub name: String,pub author: String,}
Example Test
Here's how your code will be tested:
let john = Person { name: "John".to_string(), age: 20 };assert_eq!(is_adult(&john), true);let emily = Person { name: "Emily".to_string(), age: 15 };assert_eq!(is_adult(&emily), false);
Hints
If you're having trouble, consider these hints:
<details> <summary>Click here to reveal hints</summary>- Use
&to pass references to avoid unnecessary copies. - Use the
agefield directly in a comparison for theis_adultfunction.
pub struct Person {// Define fields here// Read the description}pub fn is_adult() // Finish the function