Skip to content
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

Open
Fxlr8 opened this issue Jun 9, 2024 · 3 comments
Open

options2: unclear what to do and why #1990

Fxlr8 opened this issue Jun 9, 2024 · 3 comments
Labels
A-exercises Area: Exercises C-enhancement Category: Enhancement P-low Priority: Low

Comments

@Fxlr8
Copy link

Fxlr8 commented Jun 9, 2024

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.

    fn simple_option() {
        let target = "rustlings";
        let optional_target = Some(target);

        // TODO: Make this an if let statement whose value is "Some" type
        word = optional_target {
            assert_eq!(word, target);
        }
    }

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.

@mo8it
Copy link
Contributor

mo8it commented Jul 4, 2024

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

@Nahor
Copy link
Contributor

Nahor commented Jul 5, 2024

One problem is that it kind of looks like valid code. Coming from C++, Rust has some "weird" expressions (e.g. let ... else ... ;"). So when I first saw that code, my first thought was "what kind of Rust specific expression is that?", instead of "something is missing".

More specifically, it looks like a "normal" assignment where the rhs is optional_target{...} and so my thoughts weres like "are we creating a sort of a struct value of type optional_target?" and "are we creating a closure-like thing?".

Maybe be something like:

/* `if let` expression = optional_target*/ {
   assert_eq!(word, target);
}

@mo8it mo8it added C-enhancement Category: Enhancement A-exercises Area: Exercises P-low Priority: Low labels Sep 2, 2024
@frroossst
Copy link
Contributor

frroossst commented Sep 2, 2024

This exercise stumped if a bit too, even though I have used if-let and while-let constructs before. I think the users would benifit for more examples as to why we are doing this.

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 if let and while let demonstrating why we need the constructs, it may help make the goal of the exercise more clear, Open to edits, changes and suggestions ofcourse.

I think this would also help with #655

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-exercises Area: Exercises C-enhancement Category: Enhancement P-low Priority: Low
Projects
None yet
Development

No branches or pull requests

4 participants