mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-11 05:09:19 +00:00
reached errors
This commit is contained in:
parent
d722b0c2e3
commit
181b45d184
@ -5,14 +5,13 @@
|
||||
// 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.
|
||||
|
||||
// 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() {
|
||||
// Empty names aren't allowed.
|
||||
None
|
||||
Err(String::from("`name` was empty; it must be nonempty."))
|
||||
} else {
|
||||
Some(format!("Hi! My name is {}", name))
|
||||
Ok(format!("Hi! My name is {}", name))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
// options2.rs
|
||||
// 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)]
|
||||
mod tests {
|
||||
@ -11,7 +11,7 @@ mod tests {
|
||||
let optional_target = Some(target);
|
||||
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
@ -26,8 +26,8 @@ mod tests {
|
||||
|
||||
// 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
|
||||
integer = optional_integers.pop() {
|
||||
assert_eq!(integer, range);
|
||||
while let Some(integer) = optional_integers.pop() {
|
||||
assert_eq!(integer, Some(range));
|
||||
range -= 1;
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,8 +1,7 @@
|
||||
// options3.rs
|
||||
// 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 {
|
||||
x: i32,
|
||||
y: i32,
|
||||
@ -12,8 +11,9 @@ fn main() {
|
||||
let y: Option<Point> = Some(Point { x: 100, y: 200 });
|
||||
|
||||
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"),
|
||||
}
|
||||
|
||||
y; // Fix without deleting this line.
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user