rust-lang / rustlings

:crab: Small exercises to get you used to reading and writing Rust code!
https://rustlings.cool
MIT License
52.34k stars 9.99k forks source link

Is the Solution to Quiz 2 correct? #2045

Closed sibkru closed 1 week ago

sibkru commented 1 month ago

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 commented 1 month ago

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 commented 1 month ago
// - 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 commented 1 month ago

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 commented 1 month ago

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 commented 1 month ago

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 https://github.com/rust-lang/rustlings/issues/2045#issuecomment-2233051699 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 commented 1 month ago

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 commented 1 month ago

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

sibkru commented 1 month ago

Thanks, I appreciate you taking the time!

wezm commented 1 month ago

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 commented 1 month ago

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

mo8it commented 1 month ago

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