feat(rep): 1 lab
This commit is contained in:
1 parent
76012111bc
commit
ee49a6936e
4 files changed
+72
No files matched your search
Generated
+7
@@ -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"
|
||||
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "Learning"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
@@ -0,0 +1,11 @@
|
||||
{ pkgs ? import <nixpkgs> {} }:
|
||||
|
||||
pkgs.mkShell {
|
||||
nativeBuildInputs = with pkgs; [
|
||||
cargo
|
||||
rustc
|
||||
rustfmt
|
||||
clippy
|
||||
rust-analyzer
|
||||
];
|
||||
}
|
||||
+48
@@ -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));
|
||||
}
|
||||
Reference in new issue
Block a user