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.
miguel
1aa3484757
|
1 year ago | |
---|---|---|
.. | ||
README.md | 2 years ago | |
main.rs | 1 year ago |
README.md
drop_the_blog
Instructions
Define the following structures:
-
Blog
: containing:drops
that will save the number of dropped articles.states
that will save the state of multiple articles. If the article is not dropped, the state will befalse
, otherwise it will betrue
.
-
Article
: containing:id
asusize
.body
asString
.parent
a link to the structureBlog
. (Tip: this should be a reference).
You'll need to also add the following associated functions to the structures:
-
Blog
:new
that creates an empty blog.new_article
that receives aString
for the body and returns a tuple with theid
and a newArticle
.is_dropped
that receives anid
and returns abool
that indicates the state of the article.new_id
which returns ausize
representing the length of thestates
vector. (Which is also the first available id).add_drop
which is called by theDrop
trait. It will receive anid
that will be used to change the state of the article. If the state of that article istrue
then it will panic with the message"X is already dropped"
, whereX
represents theid
). Otherwise it should change the state totrue
and increment thedrops
field by 1.
-
Article
:new
that initializes a new article.discard
that drops the article.
You must implement the
Drop
trait for theArticle
structure. In this trait you must call the functionadd_drop
so that the state of the article changes.
Expected Functions
use std::cell::{RefCell, Cell};
#[derive(Debug, Default, Clone, Eq, PartialEq)]
pub struct Blog {
pub drops: Cell<usize>,
pub states: RefCell<Vec<bool>>
}
impl Blog {
pub fn new() -> Blog {}
pub fn new_article(&self, body: String) -> (usize, Article) {}
pub fn new_id(&self) -> usize {}
pub fn is_dropped(&self, id: usize) -> bool {}
pub fn add_drop(&self, id: usize) {}
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Article<'a> {
// expected public fields
}
impl<'a> Article<'a> {
pub fn new(id: usize, body: String, blog: &'a Blog) -> Article {}
pub fn discard(self) {}
}
Usage
Here is a program to test your function,
use drop_the_blog::*;
use std::rc::Rc;
fn main() {
let blog = Blog::new();
let (id, article) = blog.new_article(String::from("Winter is coming"));
let (id1, article1) = blog.new_article(String::from("The story of the universe"));
article.discard();
println!("{:?}", (blog.is_dropped(id), id, &blog.drops));
article1.discard();
println!("{:?}", (blog.is_dropped(id1), id1, &blog.drops));
let (id2, article2) = blog.new_article(String::from("How to cook 101"));
let article2 = Rc::new(article2);
let article2_clone = article2.clone();
drop(article2_clone);
println!("{:?}",
(blog.is_dropped(id2), id2, &blog.drops, Rc::strong_count(&article2)));
}
And its output:
$ cargo run
(true, 0, Cell { value: 1 })
(true, 1, Cell { value: 2 })
(false, 2, Cell { value: 2 }, 1)
$