-
Notifications
You must be signed in to change notification settings - Fork 10.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
options2: unclear what to do and why #1990
Comments
I agree this part of the exercise is weird. Do you have an idea how to improve it? We should still teach about the if-let statement though. You are welcome to open a PR :D |
One problem is that it kind of looks like valid code. Coming from C++, Rust has some "weird" expressions (e.g. More specifically, it looks like a "normal" assignment where the rhs is Maybe be something like: /* `if let` expression = optional_target*/ {
assert_eq!(word, target);
} |
This exercise stumped if a bit too, even though I have used fn main() {
println!("Hello, world!");
}
#[cfg(test)]
mod tests {
#[test]
fn naive() {
// you'll see options everywhere in Rust code
// they are a common abstraction and are infact
// very useful
let target = "rustlings";
let optional_target = Some(target);
// many a times you need to extract the value
// out of an option IF it is Some() or IF it
// satisfies a condition
// we often want to extract the wrapped value
// in the Some() and set it to another variable
let word: &str;
match optional_target {
Some(w) => {
word = w;
},
None => {
word = "oh no something went wrong!"
}
};
assert_eq!(word, target);
}
#[test]
fn simple_option() {
let target = "rustlings";
let optional_target = Some(target);
// we have the if let construct just for that use case
// where we lift or extract a value out of an option
if let Some(word) = optional_target {
assert_eq!(word, target)
}
// see how concise and readable our code had now
// become?
}
#[test]
fn naive_multiple() {
let mut collection: Vec<Option<i32>> = vec![
Some(1), Some(2), Some(3), Some(4), Some(0), Some(-32),
Some(45), Some(-213), Some(21321), Some(45787)
];
let mut answers: Vec<i32> = Vec::new();
// now of course we can do the naive thing and repeat the if let
// but Rust also gives us while let
//
// if you note number here has type Option<i32> shouldn't it be i32?
// well no, because if you recall .pop() return and Option, so
// collection.pop() is returning Some(Some(i32)) and while let then
// extracts the inner value to get, Some(i32),
// while let Some(number) = collection.clone().pop() {
// }
// we need to extract it once more, by doing the following
while let Some(Some(number)) = collection.pop() {
answers.push(number);
}
assert_eq!(answers.iter().sum::<i32>(), 66918);
}
#[test]
fn multiple_extractions() {
}
} I made the above code example, note that there are no exercises for the user to do, but if we provided some simple examples of I think this would also help with #655 |
Hello Rustlings Team,
First of all thank you for this project. It was really fun solving it so far. I got really stuck and scratching my head at this one and I decided to ask you for an improvement.
Looks like this excercise is trying to show how to use
Option<T>
type in Rust. Currently, the exercise seems somewhat forced and does not effectively demonstrate the real-world use cases of the Option type.We start with a "rustlings" string literal and wrap it in optional_target. Then the comment asks us to
Make this an if let statement whose value is "Some" type
(which is a riddle by itself) providing us with some convoluted assertion. I am just learning Rust and have never seen this type before. If that task had some real-world scenario showing us why Option type is useful (for example safe parsing of data) that would really help.The text was updated successfully, but these errors were encountered: