Browse Source

Fake Abstract classes in Rust

master
Julio Biason 6 months ago
parent
commit
83a5d79772
  1. 7
      abstractclasstest/Cargo.lock
  2. 6
      abstractclasstest/Cargo.toml
  3. 3
      abstractclasstest/README.md
  4. 30
      abstractclasstest/src/main.rs

7
abstractclasstest/Cargo.lock generated

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "abstractclasstest"
version = "0.1.0"

6
abstractclasstest/Cargo.toml

@ -0,0 +1,6 @@
[package]
name = "abstractclasstest"
version = "0.1.0"
edition = "2021"
[dependencies]

3
abstractclasstest/README.md

@ -0,0 +1,3 @@
# AbstractClassTest
Trying to do something akin to an abstract class in Rust.

30
abstractclasstest/src/main.rs

@ -0,0 +1,30 @@
trait Base {
fn files(&self) -> Vec<String>;
fn show_files(&self) {
for file in self.files() {
println!("file={file}");
}
}
}
struct UpcaseFiles {}
impl Base for UpcaseFiles {
fn files(&self) -> Vec<String> {
vec!["HELLO".into(), "WORLD".into()]
}
}
struct LowcaseFiles {}
impl Base for LowcaseFiles {
fn files(&self) -> Vec<String> {
vec!["hello".into(), "world".into()]
}
}
fn main() {
let f1 = UpcaseFiles {};
f1.show_files();
let f2 = LowcaseFiles {};
f2.show_files()
}
Loading…
Cancel
Save