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,}// Function to check if a person is an adultpub fn is_adult(person: &Person) -> bool { person.age >= 18}// Example Usagefn main() { let person1 = Person { name: String::from("Alice"), age: 20, }; let person2 = Person { name: String::from("Bob"), age: 16, }; assert_eq!(is_adult(&person1), true); // Alice is an adult assert_eq!(is_adult(&person2), false); // Bob is not an adult println!("All tests passed!");}
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 { // 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 { 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(x :&Person) -> bool{ if x.age > 17{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}
#[allow(dead_code)]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(person: &Person) -> bool{// Finish the function if person.age >= 18{ true }else{ false }}
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 { if person.age >= 18 { return true; } else { 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 > 17{ 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 { return true; } return false;}
pub struct Person { // Define fields here // Read the description pub name:String, pub age:u8,}pub fn is_adult(s:&Person) -> bool { // Finish the function s.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 // Finish the function Implement the function is_adult to determine if a person is an adult (18 or older).{ person.age>=18}
pub struct Person { // Define fields here // Read the description pub name: String, pub age: u8,}pub fn is_adult(pers: &Person) -> bool { pers.age >= 18} // Finish the function
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 { return false; } return true;}
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(you: &Person) -> bool { if you.age >= 18 { true } else { 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{ if person.age >= 18{ true }else{ false }}
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 // 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 { if p.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(p : &Person) -> bool { if p.age >= 18 { true } else { false }} // Finish the function
pub struct Person { pub name: String, pub age: u8}pub fn is_adult(human: &Person) -> bool { if human.age >= 18 { true } else { false }}
pub struct Person { // Define fields here pub name: String, // Read the description 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 {// 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 { if person.age > 17 { 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 { 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 { 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 { return person.age > 17}
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 { return p.age >= 18}
pub struct Person { // Define fields here pub name: String, pub age: u8, // Read the description}pub fn is_adult(pers: &Person) -> bool { // Finish the function pers.age > 17}
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