Running rust tests for Codewars challenges on local machine

I'm brand new to Rust and I'm currently working through the rust book. I wanted to work on some Codewars challenges at the same time as well. I was wondering if anyone has experience doing challenges on Codewars and how they go about organizing their project files on their local machine. Ideally I would like to be able to do all of the work and run the tests locally on my machine instead of using the Codewars editor/interface to do this. I'm curious what other people's workflows look like for doing this.

Here's an example challenge that I would like to work on: Training on From A to Z | Codewars

This is the test section from the codewars editor window:

// Add your tests here.
// See https://doc.rust-lang.org/stable/rust-by-example/testing/unit_testing.html

#[cfg(test)]
mod tests {
    use super::gimme_the_letters;
        
    fn dotest(sp: &str, expected: &str) {
        let actual = gimme_the_letters(sp);
        assert!(actual == expected, 
            "With sp = \"{sp}\"\nExpected \"{expected}\" but got \"{actual}\"")
    }

    #[test]
    fn fixed_tests() {
        dotest("a-z", "abcdefghijklmnopqrstuvwxyz");
        dotest("h-o", "hijklmno");
        dotest("Q-Z", "QRSTUVWXYZ");
        dotest("J-J", "J");
        dotest("a-b", "ab");
        dotest("A-A", "A");
        dotest("g-i", "ghi");
        dotest("H-I", "HI");
        dotest("y-z", "yz");
        dotest("e-k", "efghijk");
        dotest("a-q", "abcdefghijklmnopq");
        dotest("F-O", "FGHIJKLMNO");
    }
}

This is the provided starting code to be completed:

fn gimme_the_letters(sp: &str) -> String {
    todo!()
}
  1. What is the shortest amount of steps to take these test cases and have them run locally on my machine? Is it possible to just copy out the test case code and drop it in a file then run my function against the test cases on my own machine some how?

  2. Which file should I be putting my function and the tests into after I create a project folder with cargo new from_a_to_z ? is it src/lib.rs or src/main.rs for something like this?

  3. Is it possible using cargo to run all these tests and have the output of the results regardless of pass or fail show up in the console? I've been trying running cargo test with these but if the test passes it just says test result: ok and doesn't show me the output for each of the tests.

I'm sure a lot of this will be answered the further I get into the documentation and to be honest I just really don't know what I'm doing yet... I'm just eager to start doing some challenges and I want to make sure I'm organizing my local rust project files as efficiently as possible. Any advice would be greatly appreciated!

Once the test code is anywhere in your project, you can run it with just cargo test. That is what putting the #[test] attribute on a function does.

Oh, I see from later down that you mean including creating the project. Technically, you can just save a single file, and run rustc --test my_code.rs && ./my_code to run tests, but that won't work well for anything else — in particular you won't be able to use dependencies or get IDE assistance.

You could create modules for each problem in a single package you already created. That is fastest to make and edit.

Either works for tests, but if you don't intend to define a program that can be run by itself to do something (not counting tests), then you should not create a src/main.rs because it will force you to define a fn main(). So, run cargo new --lib instead.

cargo test -- --show-output

Thanks for taking the time to get back to me on this, I appreciate all the of the help and the various different approaches to try. This has definitely set me on the right track, cheers!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.