# Rust Usage

## Usage Instructions for Rust

For examples of how to build a Rust program which compiles and runs on Valida, see the `/valida-toolchain/examples` directory in a docker image or the `examples` directory in the release tarball. You can use any of these examples as a starting point for developing your own programs using the Valida toolchain.

You can start from an empty project as well:

1. `cargo new valida-project`.
2. `cd` into the project:

```bash
cd valida-project
```

3. Enter the Valida shell or the Docker toolchain shell, following the instructions specific to your selected [installation method](/lita-documentation/quick-start/installation-and-system-requirements.md).<br>
4. Build the project:

```
cargo +valida build --release
```

5. Run the code:

```
valida run --fast target/valida-unknown-baremetal-gnu/release/valida-project log
```

## Randomness in Rust programs

If your program uses randomness then most probably it depends on the `getrandom` crate. You can check if it does by inspecting the output of the `cargo tree` command. In that's the case a patched version of that crate must be used. Update your `Cargo.toml` by adding `patch` section:

```
[patch.crates-io]
getrandom = { git = "https://github.com/lita-xyz/getrandom.git", branch = "v0.2.16-valida" }
```

or

```
[patch.crates-io]
getrandom = { git = "https://github.com/lita-xyz/getrandom.git", branch = "v0.3.3-valida" }
```

depending on whether your program depends on `getrandom` 0.2.\* or 0.3.\*

After modifying Cargo.toml make sure that the Valida version of `getrandom` is used by running:

```
cargo update getrandom
```

Note that currently the random number generator uses a static seed and thus behaves deterministically, producing the same result on each run of a program.

## Input and output

Valida programs can interact with the environment using the input and output tapes. In the Valida zk-VM, the input tape is mapped to `stdin`, or the input file specified on the command line. The output tape is mapped to `stdout`, and the output file specified on the command line for `valida run`.&#x20;


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://lita.gitbook.io/lita-documentation/quick-start/valida-compiler-toolchain/rust-usage.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
