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 { pub name: String, pub age: u8,}pub fn is_adult(person: &Person) -> bool { person.age > 17}
pub struct Person { // Define fields here // Read the description pub name: String, pub age: u8}// Finish the functionpub 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 { p.age >= 18} // Finish the function
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 pub name: String, pub age: u8 // Read the description}pub fn is_adult(person: &Person) -> bool { if person.age >= 18 { return true } false}// Finish the function
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 { 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 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; } return false;}
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; }; return false;}// Finish the function
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 function
pub struct Person { pub name: String, pub age: u8,}pub fn is_adult(Person{age, ..}: &Person) -> bool { *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 function
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 function
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 function
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_u8}
pub struct Person { pub name: String, pub age: u8,}pub fn is_adult(person: &Person) -> bool { person.age > 17}
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 function
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,}pub 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 { p.age > 17}
pub struct Person { // Define fields here // Read the description pub name: String, pub age: i32,}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 }}
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 }}
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 { true } else { false }} // Finish the function
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 // Finish the function{ person.age >= 18}
pub struct Person { pub name: String, pub age: u8,}pub fn is_adult(person: &Person) -> bool { let Person { age, name: _ } = person; if *age >= 18 { return true; } false}
pub struct Person { // Define fields here pub name: String, pub age: u8, // Read the description}pub fn is_adult(p: &Person) -> bool{ // Finish the function if p.age < 18 {false} else {true}}
pub struct Person { pub name: String, pub age: u8}pub fn is_adult(per: &Person) -> bool { if per.age >= 18 { 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{ match person.age { res if res < 18 => false, _ => true, }}
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 } false}
pub struct Person { pub name: String, pub age: u8 // Read the description}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(person: &Person) -> bool { person.age >= 18} // Finish the function
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 as u8}
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 function
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 function
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{ // Finish the function match person.age { n if n >= 18 => return true, _ => return false, };}
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,}pub fn is_adult(person :&Person) -> bool { if *&person.age >= 18 { true } else { false }}
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} // Finish the function
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 { if person.age > 17 { return true; } false}
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 { true } else { false }} // Finish the function
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{ true } else{ false }} // Finish the function
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 { person.age >= 18}