kust

Crates.iokust
lib.rskust
version0.0.2
sourcesrc
created_at2024-12-06 12:38:53.740033
updated_at2024-12-07 18:55:35.842395
descriptionKotlin-like APIs for Rust
homepage
repositoryhttps://github.com/rChaoz/kust
max_upload_size
id1474199
size7,817
Matei Trandafir (rChaoz)

documentation

README

Kust

Write Rust like you never left Kotlin.

Kust aims to make new Rustaceans, familiar with Kotlin, feel right at home. It's also aimed to bring a few of Kotlin's goodies to the existing Rust community, without compromising on any of Rust's benefits.

Scope functions

Perform side effects and modify objects in a more concise and readable way.

Available functions:

  • using - use a value in an expression without creating a new variable (returns a new value)
  • also - perform a side effect using a value without modifying it (returns the same value)
  • apply - modify a value before returning it (returns the same value)

[!NOTE] The Kotlin let function has been renamed to using, as the let keyword is reserved in Rust.

See Kotlin documentation.

Examples

Using

use some::Person;
use kust::ScopeFunctions;

fn greet_person(encoded_person: &str) -> String {
    let name = Person::from_str(encoded_person).using(|person| format!("{} {}", person.first_name, person.last_name));
    println!("Hi, {name}!")
}

Also

use some::ComplexType;
use kust::ScopeFunctions;

fn parse_items(&items: Vec<&str>) -> Vec<ComplexType> {
    items
        // easily add a debug print for each item
        .map(|item| ComplexType::from_str(item).also(|it| println!("{it}")))
        .filter(|item| !item.is_empty())
        .collect()
}

Apply

use some::Person;
use kust::ScopeFunctions;

fn get_users() -> Vec<Person> {
    vec![
        // set an additional attribute
        Person::create("Mike").apply(|it| it.age = 34),
        Person::create("Linda").apply(|it| it.age = 25),
    ]
}
Commit count: 4

cargo fmt