Skip to content

Instantly share code, notes, and snippets.

@YangSiJun528
Last active September 17, 2025 10:04
Show Gist options
  • Select an option

  • Save YangSiJun528/10ed11237f655d53cd363d3077f9b2bd to your computer and use it in GitHub Desktop.

Select an option

Save YangSiJun528/10ed11237f655d53cd363d3077f9b2bd to your computer and use it in GitHub Desktop.
Kotlin but Rust style

Kotlin, but Rust-style

I know Kotlin and I'm learning Rust.
I just realized that Kotlin can be used in a Rust-like style.

☠️☠️☠️

data class Monster(var health: Int)
data class Wizard(val health: Int)
data class Ranger(val health: Int)
interface Magic {
fun castSpell(opponent: Monster, spellName: String = "Magic Missile") {
opponent.health -= 5
println("✨ $spellName cast! Enemy: ${opponent.health}hp")
}
}
interface FightClose
interface FightFromDistance
fun Wizard.asMagic(): Magic = object : Magic {
override fun castSpell(opponent: Monster, spellName: String) {
opponent.health -= 8
println("🧙 Wizard's $spellName! Enemy: ${opponent.health}hp")
}
}
fun Wizard.asFightClose(): FightClose = object : FightClose {}
fun Ranger.asFightClose(): FightClose = object : FightClose {}
fun Ranger.asFightFromDistance(): FightFromDistance = object : FightFromDistance {}
fun <T> attackWithBow(character: T, opponent: Monster, distance: Int)
where T : FightFromDistance {
if (distance < 10) {
opponent.health -= 10
println("🏹 Bow attack! Enemy: ${opponent.health}hp")
}
}
fun <T> attackWithSword(character: T, opponent: Monster)
where T : FightClose {
opponent.health -= 10
println("⚔️ Sword attack! Enemy: ${opponent.health}hp")
}
fun <T> fireball(character: T, opponent: Monster, distance: Int)
where T : Magic {
if (distance < 15) {
opponent.health -= 20
println("🔥 Fireball! Enemy: ${opponent.health}hp")
}
}
fun main() {
val radagast = Wizard(health = 60)
val aragorn = Ranger(health = 80)
val urukHai = Monster(health = 40)
attackWithSword(radagast.asFightClose(), urukHai)
attackWithBow(aragorn.asFightFromDistance(), urukHai, 8)
fireball(radagast.asMagic(), urukHai, 8)
// Using default implementation
radagast.asMagic().castSpell(urukHai)
}
#[derive(Debug)]
struct Monster { health: i32 }
#[derive(Debug)]
struct Wizard { health: i32 }
#[derive(Debug)]
struct Ranger { health: i32 }
trait Magic {
fn cast_spell(&self, opponent: &mut Monster, spell_name: &str) {
opponent.health -= 5;
println!("✨ {} cast! Enemy: {}hp", spell_name, opponent.health);
}
}
trait FightClose {}
trait FightFromDistance {}
impl Magic for Wizard {
fn cast_spell(&self, opponent: &mut Monster, spell_name: &str) {
opponent.health -= 8;
println!("🧙 Wizard's {}! Enemy: {}hp", spell_name, opponent.health);
}
}
impl FightClose for Wizard {}
impl FightClose for Ranger {}
impl FightFromDistance for Ranger {}
fn attack_with_bow<T: FightFromDistance>(opponent: &mut Monster, distance: u32) {
if distance < 10 {
opponent.health -= 10;
println!("🏹 Bow attack! Enemy: {}hp", opponent.health);
}
}
fn attack_with_sword<T: FightClose>(opponent: &mut Monster) {
opponent.health -= 10;
println!("⚔️ Sword attack! Enemy: {}hp", opponent.health);
}
fn fireball<T: Magic>(opponent: &mut Monster, distance: u32) {
if distance < 15 {
opponent.health -= 20;
println!("🔥 Fireball! Enemy: {}hp", opponent.health);
}
}
fn main() {
let radagast = Wizard { health: 60 };
let _aragorn = Ranger { health: 80 };
let mut uruk_hai = Monster { health: 40 };
attack_with_sword::<Wizard>(&mut uruk_hai);
attack_with_bow::<Ranger>(&mut uruk_hai, 8);
fireball::<Wizard>(&mut uruk_hai, 8);
// Using default implementation (overridden)
radagast.cast_spell(&mut uruk_hai, "Magic Missile");
}

Comments are disabled for this gist.