reached errors

This commit is contained in:
Abrar Habib 2023-03-31 15:40:49 -04:00
parent d722b0c2e3
commit 181b45d184
3 changed files with 10 additions and 11 deletions

View File

@ -5,14 +5,13 @@
// construct to `Option` that can be used to express error conditions. Let's use it! // construct to `Option` that can be used to express error conditions. Let's use it!
// Execute `rustlings hint errors1` or use the `hint` watch subcommand for a hint. // Execute `rustlings hint errors1` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
pub fn generate_nametag_text(name: String) -> Option<String> { pub fn generate_nametag_text(name: String) -> Result<String,String> {
if name.is_empty() { if name.is_empty() {
// Empty names aren't allowed. // Empty names aren't allowed.
None Err(String::from("`name` was empty; it must be nonempty."))
} else { } else {
Some(format!("Hi! My name is {}", name)) Ok(format!("Hi! My name is {}", name))
} }
} }

View File

@ -1,7 +1,7 @@
// options2.rs // options2.rs
// Execute `rustlings hint options2` or use the `hint` watch subcommand for a hint. // Execute `rustlings hint options2` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE //come back to this cause this shit made no sense to me
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
@ -11,7 +11,7 @@ mod tests {
let optional_target = Some(target); let optional_target = Some(target);
// TODO: Make this an if let statement whose value is "Some" type // TODO: Make this an if let statement whose value is "Some" type
if let Some(word) = target { if let Some(word) = optional_target {
assert_eq!(word, target); assert_eq!(word, target);
} }
} }
@ -26,8 +26,8 @@ mod tests {
// TODO: make this a while let statement - remember that vector.pop also adds another layer of Option<T> // TODO: make this a while let statement - remember that vector.pop also adds another layer of Option<T>
// You can stack `Option<T>`'s into while let and if let // You can stack `Option<T>`'s into while let and if let
integer = optional_integers.pop() { while let Some(integer) = optional_integers.pop() {
assert_eq!(integer, range); assert_eq!(integer, Some(range));
range -= 1; range -= 1;
} }
} }

View File

@ -1,8 +1,7 @@
// options3.rs // options3.rs
// Execute `rustlings hint options3` or use the `hint` watch subcommand for a hint. // Execute `rustlings hint options3` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE //ref is used to borrow the value without having to match a comletely different type of &Foo
struct Point { struct Point {
x: i32, x: i32,
y: i32, y: i32,
@ -12,8 +11,9 @@ fn main() {
let y: Option<Point> = Some(Point { x: 100, y: 200 }); let y: Option<Point> = Some(Point { x: 100, y: 200 });
match y { match y {
Some(p) => println!("Co-ordinates are {},{} ", p.x, p.y), Some(ref p) => println!("Co-ordinates are {},{} ", p.x, p.y),
_ => println!("no match"), _ => println!("no match"),
} }
y; // Fix without deleting this line. y; // Fix without deleting this line.
} }