---
title: "Shared Rust"
description: "Write validation, domain types, and encryption once in crates/shared and compile them into both your server and your app."
---

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

# Shared Rust

## Introduction

The `crates/shared` directory is an ordinary Rust crate in your workspace. Since your server and your app are both written in Rust, anything you place in this crate compiles into both of them, with no glue code.

For example, the starter crate that `init` creates includes a username rule:

```rust title="crates/shared/src/lib.rs"
/// The longest username both the server and the app accept.
pub const MAX_USERNAME_LENGTH: usize = 32;

/// Whether `name` is a valid username: 3 to 32 letters, digits, `_`, or `-`.
pub fn is_valid_username(name: &str) -> bool {
    (3..=MAX_USERNAME_LENGTH).contains(&name.chars().count())
        && name.chars().all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '-')
}
```

## Using It From Your Server

The `init` command adds `crates/shared` to your workspace, but nothing depends on it until you need it. To use it from your server, add it to the `[dependencies]` of your root `Cargo.toml`:

```toml title="Cargo.toml"
[dependencies]
shared = { path = "crates/shared" }
```

Then your server and your app may call the same function, just as they would any other crate:

```rust
if !shared::is_valid_username(&input.username) {
    // ...
}
```

> **Early release**
>
> Lairport is at v0.1. The `laravel-mobile` CLI is published on crates.io, and the `@laravel-rust-mobile/core` adapter is published on npm. Each feature is in beta or alpha: beta features are ready to try on your own apps, and alpha features are experimental. The [status](https://docs.lairport.app/status/) page lists the stage of each feature.

## What to Share

Shared code works best for the rules your server and your app must agree on. For example, you might share:

- Domain types and validation rules, so your app rejects invalid input offline using the very same rules as your server.
- Encryption and signing helpers.
- Local storage and sync logic for offline use.

## Keeping It Portable

Since this crate also compiles for phones, you should never add `laravel-rust` or other server-only crates to it. If a piece of code only makes sense on one platform, you may place it behind a `cfg` attribute, or keep it out of the crate entirely.

Source: https://docs.lairport.app/shared-rust/index.mdx
