mirror of https://github.com/01-edu/public.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
2.2 KiB
2.2 KiB
moving_targets
Instructions
You will have a linked list of Target
named Field
.
You will handle recursive types and ownership and implement the following associated functions:
new
: which will initialize theField
withhead
set toNone
.push
: which receives aTarget
and add it as aNode
at the head of the list.pop
: which returns the last addedTarget
wrapped in anOption
and removes it from the list.peek
: which returns the last addedTarget
as a reference wrapped in anOption
but do not removes it from the list.peek_mut
: which returns the last addedTarget
as a mutable reference wrapped in anOption
and also do not removes it from the list.
You must also implement a type named Link
. This will be the connection between the Field
and Target
structures. This will be a recursion type, and it must point to None
if there is no Target
to point to.
Expected Functions and structures
pub struct Field {
head: Link,
}
type Link = // To be implemented
struct Node {
elem: Target,
next: Link,
}
#[derive(Debug, PartialEq, Eq)]
pub struct Target {
pub size: u32,
pub xp: u32,
}
impl Field {
pub fn new() -> Self {}
pub fn push(&mut self, target: Target) {}
pub fn pop(&mut self) -> Option<Target> {}
pub fn peek(&self) -> Option<&Target> {}
pub fn peek_mut(&mut self) -> Option<&mut Target> {}
}
Usage
Here is a program to test your function:
use moving_targets::*;
fn main() {
let mut field = Field::new();
println!("{:?}", field.pop());
field.push(Target { size: 12, xp: 2 });
println!("{:?}", *field.peek().unwrap());
field.push(Target { size: 24, xp: 4 });
println!("{:?}", field.pop());
let last_target = field.peek_mut().unwrap();
*last_target = Target { size: 2, xp: 0 };
println!("{:?}", field.pop());
}
And its output:
$ cargo run
None
Target { size: 12, xp: 2 }
Some(Target { size: 24, xp: 4 })
Some(Target { size: 2, xp: 0 })
$