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
|
11 months ago | |
---|---|---|
.. | ||
README.md | 2 years ago | |
main.rs | 11 months ago |
README.md
profanity filter
Instructions
Sometimes it is more desirable to catch the failure of some parts of a program instead of just calling panic.
For this exercise you will have to create a message blocker, where you must block the word stupid
.
You will have to create a structure called Message
, which contains:
- elements:
content
:String
user
:String
- associated functions:
new
: which initializes the structure.send_ms
: which only has its implementation type (self) as argument. It should returnNone
if the content of the message is either empty or contains the word stupid. It should return the content of the message otherwise.
You will also need to create a function named check_ms
which accepts a reference to a Message
, and returns a tuple. This function will invoke the send_ms
function.
- If
send_ms
returnsNone
, then your function should returnfalse
and"ERROR: illegal"
. - Else, your function should return
true
and the contents of the message sent.
Expected Function
pub struct Message {
}
impl Message {
pub fn new(ms: String, u: String) -> Message {
}
pub fn send_ms(&self) -> Option<&str> {
}
}
pub fn check_ms(ms: &Message) -> (bool, &str) {
}
Usage
Here is a program to test your function
use profanity_filter::*;
fn main() {
let m0 = Message::new("hello there".to_string(), "toby".to_string());
println!("{:?}", check_ms(&m0));
let m1 = Message::new("".to_string(), "toby".to_string());
println!("{:?}", check_ms(&m1));
let m2 = Message::new("you are stupid".to_string(), "toby".to_string());
println!("{:?}", check_ms(&m2));
let m3 = Message::new("stupid".to_string(), "toby".to_string());
println!("{:?}", check_ms(&m3));
}
And its output:
$ cargo run
(true, "hello there")
(false, "ERROR: illegal")
(false, "ERROR: illegal")
(false, "ERROR: illegal")
$