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 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).pub struct Book {
pub name: String,
pub author: String,
}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);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.pub struct Person { // Define fields here // Read the description pub name: String, pub age: u8,}pub fn is_adult(person: &Person) -> bool { let min_adult_age: u8 = 18; if person.age >= min_adult_age { return true } else { return false }}pub struct Person { // Define fields here // Read the description pub name: String, pub age: u8,}pub fn is_adult(person: &Person) -> bool {// Finish the function person.age >= 18}pub struct Person { // Define fields here pub name : String, pub age : u8, // Read the description}pub fn is_adult(person: &Person) -> bool{person.age >= 18}pub struct Person { // Define fields here // Read the description pub name: String, pub age: u8,}pub fn is_adult(p: &Person) -> bool {// Finish the function p.age >= 18}pub struct Person { // Define fields here // Read the description pub name: String, pub age: u8,}pub fn is_adult(p : &Person) -> bool { p.age >= 18} // Finish the functionpub struct Person { // Define fields here // Read the description pub name: String, pub age: u8}pub fn is_adult(person: &Person) -> bool { person.age >= 18} // Finish the functionpub struct Person { // Define fields here // Read the description pub name: String, pub age: u8,}// Finish the function pub fn is_adult(person: &Person) -> bool { person.age >= 18}pub struct Person { // Define fields here pub name: String, pub age: u8, // Read the description}pub fn is_adult(person: &Person) -> bool { if person.age < 18 { return false; } return true;} // Finish the functionpub struct Person { // Define fields here // Read the description pub name: String, pub age: u8,}pub fn is_adult(p: &Person) -> bool{ if p.age < 18{ return false } else{ return true }} // Finish the functionpub struct Person { // Define fields here // Read the description pub name: String, pub age: u8,}pub fn is_adult(p: &Person) -> bool{ if p.age < 18{ return false } else{ return true }} // Finish the functionpub struct Person { // Define fields here // Read the description pub name: String, pub age: u8}pub fn is_adult(p: &Person) -> bool { // Finish the function p.age >= 18}pub struct Person { // Define fields here // Read the description pub name: String, pub age: u8}pub fn is_adult(p : &Person) -> bool { p.age >= 18}pub struct Person { // Define fields here pub name: String, pub age: u8 // Read the description}pub fn is_adult(entity: &Person) -> bool { if entity.age > 17 {return true} else {return false}}// Finish the functionpub struct Person { // Define fields here // Read the description pub name: String, pub age: u8,}pub fn is_adult(person: &Person) -> bool { person.age >= 18} // Finish the functionpub struct Person { pub name: String, pub age: u8,}pub fn is_adult(p: &Person) -> bool { if p.age >= 18 {true} else {false}} pub struct Person { pub name: String, pub age: u8,}pub fn is_adult(p: &Person) -> bool { if p.age >= 18 {true} else {false}} // Define the struct with public fieldspub struct Person { pub name: String, pub age: u8,}// Implement the function to check if the person is an adultpub fn is_adult(person: &Person) -> bool { person.age >= 18}pub struct Person { pub name: String, pub age: u8,}pub fn is_adult(p:&Person) -> bool { return p.age >= 18;}pub struct Person { pub name: String, pub age: u8,}pub fn is_adult(person: &Person) -> bool { person.age >= 18}pub struct Person { // Define fields here // Read the description pub name: String, pub age: u8,}pub fn is_adult(person: &Person) -> bool { person.age >= 18}pub struct Person { // Define fields here // Read the description pub name: String, pub age: u8,}pub fn is_adult(person: &Person) -> bool { if person.age >= 18{ return true; } else{return false};} // Finish the functionpub struct Person { // Define fields here // Read the description pub name: String, pub age: u8,}pub fn is_adult(person: &Person) -> bool { person.age >= 18} // Finish the functionpub struct Person { // Define fields here // Read the description pub name:String, pub age:u8}pub fn is_adult(s:&Person) -> bool { if s.age >= 18 { return true; } false}// Finish the functionpub struct Person { pub name: String, pub age: u8,}pub fn is_adult(p: &Person) -> bool { p.age >= 18}pub struct Person { // Define fields here pub name: String, pub age: u8, // Read the description}pub fn is_adult(person: &Person) -> bool { // Finish the function person.age >= 18}pub struct Person { // Define fields here // Read the description pub name: String, pub age: u8,}pub fn is_adult(p: &Person) -> bool { p.age >= 18}pub struct Person { // Define fields here // Read the description pub name: String, pub age: u8,}// Finish the functionpub fn is_adult(p: &Person) -> bool { p.age >= 18}pub struct Person { // Define fields here // Read the description pub name: String, pub age: u8,}pub fn is_adult(person: &Person) -> bool { person.age >= 18} // Finish the functionpub struct Person { pub name: String, pub age: u8,}pub fn is_adult(person: &Person) -> bool { if person.age >= 18 { return true; } false}pub struct Person { // Define fields here // Read the description pub name: String, pub age: u8,}pub fn is_adult(p: &Person)-> bool{ return p.age>=18;}pub struct Person { pub name: String, pub age: u8,}pub fn is_adult(person: &Person) -> bool { if person.age >= 18 { return true } return false}pub struct Person { // Define fields here // Read the description pub name: String, pub age: u8,}pub fn is_adult(p: &Person) -> bool { if p.age >= 18 { true } else { false }} // Finish the functionpub struct Person { pub name: String, pub age: u8,}pub fn is_adult(person: &Person) -> bool { return person.age >= 18;}pub struct Person { // Define fields here // Read the description pub name: String, pub age: u8,}pub fn is_adult(person: &Person) ->bool// Finish the function{ if person.age >= 18 { return true; } else{ return false; }}pub struct Person { // Define fields here pub name: String, pub age: u8, // Read the description}pub fn is_adult(person: &Person) -> bool{ person.age >= 18} // Finish the functionpub struct Person { pub name: String, pub age: u8,}pub fn is_adult(person: &Person) -> bool { person.age >= 18}pub struct Person { // Define fields here // Read the description pub name: String, pub age: u8,}pub fn is_adult(person: &Person) -> bool {// Finish the function if person.age < 18 { false } else { true }}pub struct Person { pub name: String, pub age: u8,}pub fn is_adult(person: &Person) -> bool { person.age >= 18}pub struct Person { pub name: String, pub age: u8, // Define fields here // Read the description}pub fn is_adult(person: &Person) -> bool { if person.age >= 18 { true } else { false }} // Finish the functionpub struct Person { pub name: String, pub age: u8,}pub fn is_adult(person: &Person) -> bool { if person.age < 18 { return false } return true}pub struct Person { pub name: String, pub age: u8}pub fn is_adult(person: &Person) -> bool { person.age >= 18} // Finish the functionpub struct Person { // Define fields here // Read the description pub name: String, pub age: u8,}pub fn is_adult(p: &Person) -> bool { p.age >= 18}// Define the struct with public fieldspub struct Person { pub name: String, pub age: u8,}// Function that checks if the person is an adultpub fn is_adult(person: &Person) -> bool { person.age >= 18}#[warn(dead_code)]pub struct Person { // Define fields here // Read the description pub name : String, pub age : u8}pub fn is_adult(person: &Person)-> bool{ // Finish the function if person.age >= 18 { return true; } return false;}pub struct Person { // Define fields here pub name: String, pub age: u8, // Read the description}pub fn is_adult(person: &Person)->bool{ person.age >= 18}pub struct Person { // Define fields here // Read the description pub name : String, pub age :u8,}pub fn is_adult(a :&Person) -> bool{ a.age >= 18}// Finish the functionpub struct Person { // Define fields here // Read the description pub name: String, pub age: u8,}pub fn is_adult(p: &Person) -> bool { p.age >= 18}pub struct Person { pub name: String, pub age: u8,}pub fn is_adult(p: &Person) -> bool { p.age >= 18}pub struct Person { // Define fields here pub name: String, pub age: u8, // Read the description}pub fn is_adult(human: &Person) -> bool { if human.age >= 18 { true } else { false }} // Finish the functionpub struct Person { pub name: String, pub age: u8 // Define fields here // Read the description}pub fn is_adult(someone: &Person) -> bool {// Finish the function someone.age >= 18}