Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Kotlin

Generates Kotlin data classes with kotlinx.serialization.

Type Mappings

Rust TypeKotlin Type
StringString
boolBoolean
u8, u16, u32, u64UByte, UShort, UInt, ULong
i8, i16, i32, i64Byte, Short, Int, Long
f32, f64Float, Double
UuidString
DateTime<Utc>kotlinx.datetime.Instant
Option<T>T? = null
Vec<T>List<T>
HashMap<K, V>Map<K, V>

Example

Rust:

#![allow(unused)]
fn main() {
#[derive(TypeWriter)]
#[sync_to(kotlin)]
pub struct User {
    pub id: String,
    pub email: String,
    pub age: Option<u32>,
}
}

Generated:

package types

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class User(
    @SerialName("id") val id: String,
    @SerialName("email") val email: String,
    @SerialName("age") val age: Int? = null
)

Package Name

Configure the Kotlin package name:

[kotlin]
package_name = "com.example.models"

File Naming

Files use PascalCase by default: UserProfileUserProfile.kt

[kotlin]
file_style = "snake_case"  # user_profile.kt