diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..4918a32 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "Learning" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..6119f2f --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "Learning" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/shell.nix b/shell.nix new file mode 100644 index 0000000..b84f152 --- /dev/null +++ b/shell.nix @@ -0,0 +1,11 @@ +{ pkgs ? import {} }: + +pkgs.mkShell { + nativeBuildInputs = with pkgs; [ + cargo + rustc + rustfmt + clippy + rust-analyzer + ]; +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..d6e144d --- /dev/null +++ b/src/main.rs @@ -0,0 +1,48 @@ +enum MATH { + ADD, + SUB, + MUL, + DEV, +} + +struct Math { + a: i32, + b: i32, +} + +impl Math { + fn init(a: i32, b: i32) -> Math { + return Math { a, b }; + } + + fn calculate(&self, operation: MATH) -> i32 { + match operation { + MATH::ADD => return self.a + self.b, + MATH::SUB => return self.a - self.b, + MATH::MUL => return self.a * self.b, + MATH::DEV => { + if self.b == 0 { + return 0; + } else { + return self.a / self.b; + } + } + } + } + + fn reinit(&mut self, a: i32, b: i32) { + self.a = a; + self.b = b; + } +} + +fn main() { + let operation: MATH = MATH::ADD; + let mut calculator: Math = Math::init(10, 4); + + print!("ans: {}\n", calculator.calculate(operation)); + + calculator.reinit(32, 2); + + println!("ans after reinit: {}", calculator.calculate(MATH::DEV)); +}