> ## Documentation Index
> Fetch the complete documentation index at: https://docs.codecrafters.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Rust

> Learn how to solve CodeCrafters challenges in Rust

<Note>
  **New to Rust?** We recommend checking out [Rust by
  Example](https://doc.rust-lang.org/rust-by-example/) to get up to speed.
</Note>

## Local development

To run the code for your challenge locally, you'll need `cargo` installed. Our test runners use version `1.76` (as of April 2024).

The script for your challenge (like `./your_bittorrent.sh`) will automatically compile and run your code using `cargo run`. It'll
look something like this:

```sh theme={null}
#!/bin/sh

exec cargo run \
    --quiet \
    --release \
    --target-dir=/tmp/codecrafters-bittorrent-target \
    --manifest-path $(dirname "$0")/Cargo.toml -- "$@"
```

## File structure

* The files for your solution are placed in the `src` directory.
* `src/main.rs` contains the `main` function, which is what the test runner executes.

## Adding more files

You can add more files and directories to the `src` directory. The test runner will include them when compiling your code.

Rust uses [modules](https://doc.rust-lang.org/rust-by-example/mod.html) to organize code. For example, to define the `foo` module, create a file at `src/foo.rs`.

You can use functions and variables from `foo.rs` in your main code like so:

<CodeGroup>
  ```Rust src/main.rs theme={null}
  mod foo;

  fn main() {
    foo::foo(); // Calls the foo function from foo.rs
  }
  ```

  ```Rust src/foo.rs theme={null}
  pub fn foo() {
      println!("Hello World!");
  }
  ```
</CodeGroup>

<Note>
  Remember to make `foo()` accessible to `main.rs` by adding the `pub`
  identifier
</Note>

Refer to the Rust [documentation](https://doc.rust-lang.org/book/ch07-05-separating-modules-into-different-files.html) for more details.

## Adding dependencies

You can add dependencies to your program using `cargo add`.

For example, to add the `rand` crate, run the following command:

```bash theme={null}
cargo add rand
```

This command will automatically find the latest version of the `rand` crate and add it to your `Cargo.toml` file:

```toml theme={null}
[dependencies]
rand = "0.8.5"
```

It'll also make changes to your `Cargo.lock` file.
