Skip to main content

Yazi Kitty Gid Animated Code

Oct 5, 2024.michelin
Loading...

More Rust Posts

Inheritance

Oct 6, 2024LeifMessinger

0 likes • 10 views

trait ATrait {
fn a(&mut self) -> &mut A{
self.a()
}
fn b(&mut self) -> &mut i64{
return self.a().b()
}
}
struct A {
b: i64
}
impl ATrait for A {
fn a(&mut self) -> &mut A{
self
}
fn b(&mut self) -> &mut i64{
&mut self.b
}
}
struct B {
a: A,
c: i64
}
impl ATrait for B{
fn a(&mut self) -> &mut A{
self.a.a()
}
}
fn main() {
let mut b: B = B{
a: A {b: 2},
c: 3
};
println!("{}", b.b());
}

Rust 99 Bottles

Aug 15, 2023LeifMessinger

1 like • 17 views

struct Beer {
bottles: i32,
}
impl Beer {
fn new() -> Beer {
Beer { bottles: 99 }
}
fn take_one_down(&mut self) {
self.bottles -= 1;
}
fn no_more(&self) -> bool {
self.bottles == 0
}
fn go_to_the_store(&mut self) {
self.bottles = 99;
}
fn to_string(&self) -> String {
match self.bottles {
0 => "No more bottles".to_string(), //Reaized this is incorrect because the N could be lowercase. But technically the comma in the beer song is incorrect because it separates two whole sentences and should be a comma
1 => "1 bottle".to_string(),
_ => format!("{} bottles", self.bottles),
}
}
}
fn main() {
let mut bottles: Beer = Beer::new();
let KEEP_GOING: bool = true;
let FOREVER: bool = false;
let MAX_LOOPS: i32 = 2;
let mut int: i32 = 0;
while FOREVER | (int < MAX_LOOPS){
print!("{} of beer on the wall, ", bottles.to_string());
println!("{} of beer.", bottles.to_string());
if !bottles.no_more(){
print!("Take one down, pass it around, ");
bottles.take_one_down();
println!("{} of beer on the wall.", bottles.to_string());
println!();
} else {
print!("Go to the store and buy some more, ");
bottles.go_to_the_store();
println!("{} of beer on the wall.", bottles.to_string());
if(!KEEP_GOING){
break; //Comment this out to keep it going
}else{
println!();
int += 1;
}
}
}
}