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

Is the Solution to Quiz 2 correct? #2045

Closed
sibkru opened this issue Jul 12, 2024 · 11 comments
Closed

Is the Solution to Quiz 2 correct? #2045

sibkru opened this issue Jul 12, 2024 · 11 comments

Comments

@sibkru
Copy link

sibkru commented Jul 12, 2024

I'm new to Rust, so I might be doing something wrong.

In Quiz 2, I was struggling with the following error:

pub fn transformer(input: Vec<(String, Command)>) -> Vec {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private type

The solution seems to be to make the Command Enum public.
pub enum Command { Uppercase, Trim, Append(usize), }
I wasn't sure if that was a hack or the correct way, so I looked into the solution, which does not declare the enum as public.
But when I copy and paste the solution and look at the rustlings output, I get the same error message I got with my own code (now also for transformer_iter).

rustlings version: 6.0.1
rustc version: 1.79
cargo version: 1.79

Is the solution incorrect, or am I doing something wrong on my side (probably the latter)?

@mo8it
Copy link
Contributor

mo8it commented Jul 12, 2024

You are probably doing something wrong because the solutions are checked in the CI to all compile and run correctly.

Can you please paste your whole exercise code?

@sibkru
Copy link
Author

sibkru commented Jul 17, 2024

// - Strings
// - Vecs
// - Move semantics
// - Modules
// - Enums
//
// Let's build a little machine in the form of a function. As input, we're going
// to give a list of strings and commands. These commands determine what action
// is going to be applied to the string. It can either be:
// - Uppercase the string
// - Trim the string
// - Append "bar" to the string a specified amount of times
//
// The exact form of this will be:
// - The input is going to be a Vector of 2-length tuples,
//   the first element is the string, the second one is the command.
// - The output element is going to be a vector of strings.

enum Command {
    Uppercase,
    Trim,
    Append(usize),
}

mod my_module {
    use super::Command;

    // TODO: Complete the function.
    // pub fn transformer(input: ???) -> ??? { ??? }
    pub fn transformer(input: Vec<(String, Command)>) -> Vec<String> {
        let mut result = vec![];
        for line in input.into_iter() {
            match line {
                (a, Command::Uppercase) => {
                    result.push(a.to_uppercase());
                }
                (a, Command::Trim) => {
                    result.push(a.trim().to_string());
                }
                (a, Command::Append(num)) => {
                    result.push(a + &"bar".repeat(num));
                }
            };
        }
        result
    }
}

fn main() {
    // You can optionally experiment here.
}

#[cfg(test)]
mod tests {
    // TODO: What do we need to import to have `transformer` in scope?
    // use ???;
    use super::my_module::transformer;
    use super::Command;

    #[test]
    fn it_works() {
        let input = vec![
            ("hello".to_string(), Command::Uppercase),
            (" all roads lead to rome! ".to_string(), Command::Trim),
            ("foo".to_string(), Command::Append(1)),
            ("bar".to_string(), Command::Append(5)),
        ];
        let output = transformer(input);

        assert_eq!(
            output,
            [
                "HELLO",
                "all roads lead to rome!",
                "foobar",
                "barbarbarbarbarbar",
            ]
        );
    }
}

This is my complete exercise code that yields the above-described error.
When I copy the transformer function from here: https://github.com/rust-lang/rustlings/blob/main/solutions/quizzes/quiz2.rs
I get the same error.

@mo8it
Copy link
Contributor

mo8it commented Jul 17, 2024

I can't reproduce it. Very weird.

I also pasted it into the Rust playground and it ran without any problem: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=50188eeb2b80684521e1c7c73282c239

Are you sure cargo --version returns the version 1.79?

Can you please run rustup update and try again?

Can you please also share a screenshot of the error in Rustlings?

@sibkru
Copy link
Author

sibkru commented Jul 17, 2024

That's really weird. How could I have broken my rust settings?
image

image

I also listed my installed toolchains, I only have
stable-aarch64-apple-darwin installed.

I also tried switching to a different exercise in Rustlings and back to test whether Rustlings did not update its error message for some reason, but the error appeared again.

Thank you for your help

@mo8it
Copy link
Contributor

mo8it commented Jul 17, 2024

I just ran the checks in the CI on Apple. All solutions ran without a problem:
https://github.com/rust-lang/rustlings/actions/runs/9974536710/job/27562222308?pr=2052

Can you please create a new project with cargo new quiz2 in some directory, paste your code in #2045 (comment) and run it with cargo clippy and cargo test? Please also do the same with the solution file. This allows us to separate the problem from Rustlings.

@sibkru
Copy link
Author

sibkru commented Jul 17, 2024

Here are the results:
on my exercise:
cargo clippy produces the same error.
cargo test runs fine.
image
image

on solution file:
cargo clippy produces the error.
cargo test runs fine.

image image

@mo8it
Copy link
Contributor

mo8it commented Jul 17, 2024

I asked for help on the Rust forum: https://users.rust-lang.org/t/error-only-on-a-specific-target/114552

@sibkru
Copy link
Author

sibkru commented Jul 17, 2024

Thanks, I appreciate you taking the time!

@wezm
Copy link
Member

wezm commented Jul 17, 2024

cargo clippy produces the error.
cargo test runs fine.

@sibkru since it is clippy that is failing it might be worth including the output of:

which cargo-clippy

and

cargo clippy --version

as well.

@mo8it
Copy link
Contributor

mo8it commented Jul 17, 2024

Oh, good idea! It is also worth trying out cargo check.

@mo8it
Copy link
Contributor

mo8it commented Jul 30, 2024

@sibkru Did you find time to try the commands above? :)

@mo8it mo8it closed this as completed Aug 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants