Rust Target Overview
Genotype generates idiomatic Rust code that can be used directly in the application or published as a package.
This overview shows how Genotype types translate to Rust, target-specific features and configuration options.
See the Quick Genotype Language Tour for the language overview and Rust Configuration for detailed configuration reference.
Translation
Section titled “Translation”The following examples give an overview of how Genotype translates into Rust.
Complete Module
Section titled “Complete Module”This source combines an object, a literal field, an optional field, a record, and any:
Book: { kind: "book", displayTitle: string, subtitle?: string, ratings: { []: int }, extra: any,}The generated module includes its type definitions and imports:
use std::collections::BTreeMap;use genotype_runtime::Any;use litty::serde_literals;use serde::{Deserialize, Serialize};
#[serde_literals]#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]#[literals(kind = "book")]pub struct Book { #[serde(rename = "displayTitle")] pub display_title: String, #[serde(default, skip_serializing_if = "Option::is_none")] pub subtitle: Option<String>, pub ratings: BTreeMap<String, i64>, pub extra: Any,}Unions
Section titled “Unions”Genotype unions translate into Rust enums. Non-literal unions use untagged serialization to preserve the original data shape.
Value: string | int#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]#[serde(untagged)]pub enum Value { String(String), Int(i64),}Primitives
Section titled “Primitives”Numeric Types
Section titled “Numeric Types”number
Section titled “number”Genotype number translates into the Rust f64 type.
Amount: numberpub type Amount = f64;Genotype int translates into the Rust i64 type.
Count: intpub type Count = i64;Genotype float translates into the Rust f64 type.
Ratio: floatpub type Ratio = f64;Sized Numeric Types
Section titled “Sized Numeric Types”Sized Genotype numeric types translate directly to their Rust counterparts.
SmallCount: i16PreciseRatio: f32LargeCount: i128pub type SmallCount = i16;
pub type PreciseRatio = f32;
pub type LargeCount = i128;Booleans
Section titled “Booleans”Genotype boolean translates into the Rust bool type.
Ready: booleanpub type Ready = bool;Strings
Section titled “Strings”Genotype string translates into the Rust owned String type.
Title: stringpub type Title = String;Literal Types
Section titled “Literal Types”Genotype literal types translate into named Rust structs with attributes that preserve the exact serialized value.
Category: "fiction"#[serde_literal("fiction")]#[derive(Serialize, Deserialize)]pub struct Category;Genotype null translates into a Rust type that serializes as null.
Empty: null#[serde_literal(null)]#[derive(Serialize, Deserialize)]pub struct Empty;Branded Primitives
Section titled “Branded Primitives”Genotype branded primitives translate into Rust newtype structs, distinct from their underlying primitive.
BookId: @string#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]pub struct BookId(pub String);Composite Types
Section titled “Composite Types”Objects
Section titled “Objects”Genotype objects translate into public Rust structs with Serde support. Nested objects become separate named structs.
Book: { title: string }#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]pub struct Book { pub title: String,}Optional Object Fields
Section titled “Optional Object Fields”Optional Genotype object fields translate into Rust Option<T> fields. Absent values are omitted during serialization.
Draft: { subtitle?: string }#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]pub struct Draft { #[serde(default, skip_serializing_if = "Option::is_none")] pub subtitle: Option<String>,}Object Extensions
Section titled “Object Extensions”Genotype object extensions translate into Rust structs containing the base struct’s fields.
Named: { name: string }NamedBook: { ...Named, pages: int }#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]pub struct Named { pub name: String,}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]pub struct NamedBook { pub name: String, pub pages: i64,}Arrays
Section titled “Arrays”Genotype arrays translate into Rust Vec<T> types.
Titles: [string]pub type Titles = Vec<String>;Tuples
Section titled “Tuples”Genotype tuples translate directly to Rust tuple types.
Point: (float, float)pub type Point = (f64, f64);Records
Section titled “Records”Genotype records translate into Rust BTreeMap<K, V> types. An omitted key type means string keys.
Scores: { []: int }pub type Scores = BTreeMap<String, i64>;Special Data Types
Section titled “Special Data Types”Any Type
Section titled “Any Type”Genotype any translates into the Genotype runtime’s Any type.
Payload: anypub type Payload = Any;Generic Types
Section titled “Generic Types”Genotype generic types translate into Rust generic types.
Envelope<Body>: { body: Body }#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]pub struct Envelope<Body> { pub body: Body,}Recursive Types
Section titled “Recursive Types”Genotype recursive types translate into Rust types with Box indirection where needed to give them a finite size.
LinkedNode: { value: string, next?: LinkedNode }#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]pub struct LinkedNode { pub value: String, #[serde(default, skip_serializing_if = "Option::is_none")] pub next: Option<Box<LinkedNode>>,}Annotations
Section titled “Annotations”Annotations let you customize enum variants directly in Genotype:
Status: | #[variant = "Available"] "in_stock" | "sold_out"Here variant names the Rust variant Available. Its serialized value remains "in_stock".
Configuration
Section titled “Configuration”Use [rs] in genotype.toml to configure Rust. See the Rust Configuration Reference for more details.
See Genotype Configuration for global settings and Common Target Options.