Решение на упр.03 задача 2 от Ясин Йосифов
Резултати
- 1 точка от тестове
- 0 бонус точки
- 1 точка общо
- 1 успешни тест(а)
- 0 неуспешни тест(а)
Код
enum VehicleKind {
    Car,
    Truck,
    Motorcycle,
    Bicycle,
}
struct Vehicle {
    kind: VehicleKind,
    fuel: f64,
    distance: f64,
}
enum DriveError {
    NotEnoughFuel { needed: f64, have: f64 }
}
impl Vehicle {
    fn new(kind: VehicleKind, fuel: f64) -> Self {
        Self {
            kind: kind,
            fuel: fuel,
            distance: 0.0
        }
    }
    fn drive(&mut self, km: f64) -> Result<(), DriveError> {
        let mut lpkm: f64 = 0.0;
        match self.kind {
            VehicleKind::Car => lpkm = 0.07,
            VehicleKind::Truck => lpkm = 0.15,
            VehicleKind::Motorcycle => lpkm = 0.05,
            VehicleKind::Bicycle => lpkm = 0.00,
        }
        let needed = km * lpkm;
        if needed > self.fuel {
            Err(DriveError::NotEnoughFuel { needed: needed, have: self.fuel })
        } else {
            self.fuel -= needed;
            self.distance += km;
            Ok(())
        }
    }
}
Лог от изпълнението
Updating crates.io index
     Locking 17 packages to latest compatible versions
   Compiling proc-macro2 v1.0.103
   Compiling unicode-ident v1.0.22
   Compiling quote v1.0.41
   Compiling futures-sink v0.3.31
   Compiling futures-core v0.3.31
   Compiling futures-channel v0.3.31
   Compiling memchr v2.7.6
   Compiling syn v2.0.108
   Compiling pin-project-lite v0.2.16
   Compiling slab v0.4.11
   Compiling futures-io v0.3.31
   Compiling futures-task v0.3.31
   Compiling pin-utils v0.1.0
   Compiling solution v0.1.0 (/tmp/d20251030-1757769-1x8527i/solution)
warning: value assigned to `lpkm` is never read
  --> src/lib.rs:28:17
   |
28 |         let mut lpkm: f64 = 0.0;
   |                 ^^^^
   |
   = help: maybe it is overwritten before being read?
   = note: `#[warn(unused_assignments)]` on by default
warning: enum `VehicleKind` is never used
 --> src/lib.rs:1:6
  |
1 | enum VehicleKind {
  |      ^^^^^^^^^^^
  |
  = note: `#[warn(dead_code)]` on by default
warning: struct `Vehicle` is never constructed
 --> src/lib.rs:8:8
  |
8 | struct Vehicle {
  |        ^^^^^^^
warning: enum `DriveError` is never used
  --> src/lib.rs:14:6
   |
14 | enum DriveError {
   |      ^^^^^^^^^^
warning: associated items `new` and `drive` are never used
  --> src/lib.rs:19:8
   |
18 | impl Vehicle {
   | ------------ associated items in this implementation
19 |     fn new(kind: VehicleKind, fuel: f64) -> Self {
   |        ^^^
...
27 |     fn drive(&mut self, km: f64) -> Result<(), DriveError> {
   |        ^^^^^
warning: `solution` (lib) generated 5 warnings
   Compiling futures-macro v0.3.31
   Compiling futures-util v0.3.31
   Compiling futures-executor v0.3.31
   Compiling futures v0.3.31
warning: value assigned to `lpkm` is never read
  --> tests/../src/lib.rs:28:17
   |
28 |         let mut lpkm: f64 = 0.0;
   |                 ^^^^
   |
   = help: maybe it is overwritten before being read?
   = note: `#[warn(unused_assignments)]` on by default
warning: variants `Truck`, `Motorcycle`, and `Bicycle` are never constructed
 --> tests/../src/lib.rs:3:5
  |
1 | enum VehicleKind {
  |      ----------- variants in this enum
2 |     Car,
3 |     Truck,
  |     ^^^^^
4 |     Motorcycle,
  |     ^^^^^^^^^^
5 |     Bicycle,
  |     ^^^^^^^
  |
  = note: `#[warn(dead_code)]` on by default
warning: fields `needed` and `have` are never read
  --> tests/../src/lib.rs:15:21
   |
15 |     NotEnoughFuel { needed: f64, have: f64 }
   |     -------------   ^^^^^^       ^^^^
   |     |
   |     fields in this variant
warning: `solution` (test "solution_test") generated 3 warnings
    Finished `test` profile [unoptimized + debuginfo] target(s) in 8.54s
     Running tests/solution_test.rs (target/debug/deps/solution_test-bfd50394249726db)
running 1 test
test solution_test::test_basic ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
История (2 версии и 0 коментара)
Ясин качи решение на 23.10.2025 19:02 (преди 8 дена)
 enum VehicleKind {
     Car,
     Truck,
     Motorcycle,
     Bicycle,
 }
 struct Vehicle {
     kind: VehicleKind,
     fuel: f64,
     distance: f64,
 }
 enum DriveError {
     NotEnoughFuel { needed: f64, have: f64 }
 }
 impl Vehicle {
     fn new(kind: VehicleKind, fuel: f64) -> Self {
         Self {
             kind: kind,
             fuel: fuel,
             distance: 0.0
         }
     }
     fn drive(&mut self, km: f64) -> Result<(), DriveError> {
         let mut lpkm: f64 = 0.0;
         match self.kind {
             VehicleKind::Car => lpkm = 0.07,
             VehicleKind::Truck => lpkm = 0.15,
             VehicleKind::Motorcycle => lpkm = 0.05,
             VehicleKind::Bicycle => lpkm = 0.00,
         }
         let needed = km * lpkm;
         if needed > self.fuel {
             Err(DriveError::NotEnoughFuel { needed: needed, have: self.fuel })
         } else {
             self.fuel -= needed;
+            self.distance += km;
             Ok(())
         }
     }
 }
