Skip to content

Instantly share code, notes, and snippets.

@Davis-3450
Created August 17, 2025 00:13
Show Gist options
  • Save Davis-3450/24247cc6c402058296f63d6d8110d078 to your computer and use it in GitHub Desktop.
Save Davis-3450/24247cc6c402058296f63d6d8110d078 to your computer and use it in GitHub Desktop.
Rustlings structs 2
// Clone trait
// structs by default don't allow cloning, if we add this trait we can allow it and implement an alternative fix to this
#[derive(Debug, Clone)]
struct Order {
name: String,
year: u32,
made_by_phone: bool,
made_by_mobile: bool,
made_by_email: bool,
item_number: u32,
count: u32,
}
fn create_order_template() -> Order {
Order {
name: String::from("Bob"),
year: 2019,
made_by_phone: false,
made_by_mobile: false,
made_by_email: true,
item_number: 123,
count: 0,
}
}
fn main() {
// You can optionally experiment here.
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn your_order() {
let order_template = create_order_template();
// TODO: Create your own order using the update syntax and template above!
// let your_order =
let mut your_order = order_template.clone();
your_order.name = "Hacker in Rust".to_string();
your_order.count = 1;
assert_eq!(your_order.name, "Hacker in Rust");
assert_eq!(your_order.year, order_template.year);
assert_eq!(your_order.made_by_phone, order_template.made_by_phone);
assert_eq!(your_order.made_by_mobile, order_template.made_by_mobile);
assert_eq!(your_order.made_by_email, order_template.made_by_email);
assert_eq!(your_order.item_number, order_template.item_number);
assert_eq!(your_order.count, 1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment