Rust code compiles fine on my computer but not in Exercism editor. What Have I done incorrectly?

I’m attempting to use the Exercism platformto learn some Rust coding. The second exercise is to produce a function that reverses and prints a string. This is all new so I found some Rust code that will do this, but when I plug it into the Exercism Rust gui, and run tests it throws errors. However on my own machine, when I create and compile a rust (.rs) file containing the same exact code it compiles and runs just fine. How can this be? Does the exercism gui allow only one correct solution? Thanks, J.

Are you running the tests locally?

What is your code? What is the error?

This is the code:

fn reverse_alternate(initial: &str) → String {
// Get chars from string.
let chars: Vec = initial.chars().collect();
// Allocate new string
let mut reversed = String::new();
// Add chars in reverse order, stopping at 0.
let mut index = initial.len() - 1;
loop {
reversed.push(chars[index]);
if index == 0 {
break;
}
index -= 1;
}
reversed
}

fn main() {
let reversed = reverse_alternate(“cat”);
println!(“{reversed}”);
}

and this is the error from tests:

We received the following error when we ran your code:

   Compiling reverse_string v1.2.0 (exercism-iteration)
warning: function `reverse_alternate` is never used
 --> src/lib.rs:1:4
  |
1 | fn reverse_alternate(initial: &str) -> String {
  |    ^^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(dead_code)]` on by default

warning: function `main` is never used
  --> src/lib.rs:18:4
   |
18 | fn main() {
   |    ^^^^

warning: `reverse_string` (lib) generated 2 warnings
warning: unused import: `reverse_string::*`
 --> tests/reverse-string.rs:1:5
  |
1 | use reverse_string::*;
  |     ^^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

error[E0425]: cannot find function `reverse` in this scope
 --> tests/reverse-string.rs:6:18
  |
6 |     let output = reverse(input);
  |                  ^^^^^^^ not found in this scope

error[E0425]: cannot find function `reverse` in this scope
  --> tests/reverse-string.rs:15:18
   |
15 |     let output = reverse(input);
   |                  ^^^^^^^ not found in this scope

error[E0425]: cannot find function `reverse` in this scope
  --> tests/reverse-string.rs:24:18
   |
24 |     let output = reverse(input);
   |                  ^^^^^^^ not found in this scope

error[E0425]: cannot find function `reverse` in this scope
  --> tests/reverse-string.rs:33:18
   |
33 |     let output = reverse(input);
   |                  ^^^^^^^ not found in this scope

error[E0425]: cannot find function `reverse` in this scope
  --> tests/reverse-string.rs:42:18
   |
42 |     let output = reverse(input);
   |                  ^^^^^^^ not found in this scope

error[E0425]: cannot find function `reverse` in this scope
  --> tests/reverse-string.rs:51:18
   |
51 |     let output = reverse(input);
   |                  ^^^^^^^ not found in this scope

error[E0425]: cannot find function `reverse` in this scope
  --> tests/reverse-string.rs:60:18
   |
60 |     let output = reverse(input);
   |                  ^^^^^^^ not found in this scope

For more information about this error, try `rustc --explain E0425`.
warning: `reverse_string` (test "reverse-string") generated 1 warning
error: could not compile `reverse_string` due to 7 previous errors; 1 warning emitted
warning: build failed, waiting for other jobs to finish...

thx. J

The error tells you that you (1) define functions that are never called and (2) are missing the expected functions that do need to be defined! The exercise runs tests which expects specific functions and structures. You can’t define arbitrary functions.

To be specific, Exercism won’t run your main function. It’ll run the function specified in the tests. And as it won’t run your main function, both that and the reverse_alternate are unused.