Решение на упр.03 задача 2 от Иван Коджабашев

Обратно към всички решения

Към профила на Иван Коджабашев

Резултати

  • 1 точка от тестове
  • 0 бонус точки
  • 1 точка общо
  • 1 успешни тест(а)
  • 0 неуспешни тест(а)

Код

#[derive(Debug)]
enum VehicleKind {
Car,
Truck,
Motorcycle,
Bicycle,
}
#[derive(Debug)]
struct Vehicle {
kind: VehicleKind,
fuel: f64,
distance: f64,
}
#[derive(Debug)]
enum DriveError {
NotEnoughFuel { needed: f64, have: f64 },
}
impl Vehicle {
fn new(kind: VehicleKind, fuel: f64) -> Self {
Self {
kind,
fuel,
distance: 0.0,
}
}
fn drive(&mut self, km: f64) -> Result<(), DriveError> {
let consumption = match self.kind {
VehicleKind::Car => 0.07,
VehicleKind::Truck => 0.15,
VehicleKind::Motorcycle => 0.05,
VehicleKind::Bicycle => 0.0,
};
let needed = km * consumption;
if needed > self.fuel {
return Err(DriveError::NotEnoughFuel {
needed,
have: self.fuel,
});
}
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 pin-project-lite v0.2.16
   Compiling futures-task v0.3.31
   Compiling syn v2.0.108
   Compiling memchr v2.7.6
   Compiling slab v0.4.11
   Compiling pin-utils v0.1.0
   Compiling futures-io v0.3.31
   Compiling solution v0.1.0 (/tmp/d20251030-1757769-daf16r/solution)
warning: enum `VehicleKind` is never used
 --> src/lib.rs:2:6
  |
2 | enum VehicleKind {
  |      ^^^^^^^^^^^
  |
  = note: `#[warn(dead_code)]` on by default

warning: struct `Vehicle` is never constructed
  --> src/lib.rs:10:8
   |
10 | struct Vehicle {
   |        ^^^^^^^

warning: enum `DriveError` is never used
  --> src/lib.rs:17:6
   |
17 | enum DriveError {
   |      ^^^^^^^^^^

warning: associated items `new` and `drive` are never used
  --> src/lib.rs:22:8
   |
21 | impl Vehicle {
   | ------------ associated items in this implementation
22 |     fn new(kind: VehicleKind, fuel: f64) -> Self {
   |        ^^^
...
30 |     fn drive(&mut self, km: f64) -> Result<(), DriveError> {
   |        ^^^^^

warning: `solution` (lib) generated 4 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: variants `Truck`, `Motorcycle`, and `Bicycle` are never constructed
 --> tests/../src/lib.rs:4:5
  |
2 | enum VehicleKind {
  |      ----------- variants in this enum
3 |     Car,
4 |     Truck,
  |     ^^^^^
5 |     Motorcycle,
  |     ^^^^^^^^^^
6 |     Bicycle,
  |     ^^^^^^^
  |
  = note: `VehicleKind` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis
  = note: `#[warn(dead_code)]` on by default

warning: fields `needed` and `have` are never read
  --> tests/../src/lib.rs:18:21
   |
18 |     NotEnoughFuel { needed: f64, have: f64 },
   |     -------------   ^^^^^^       ^^^^
   |     |
   |     fields in this variant
   |
   = note: `DriveError` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis

warning: `solution` (test "solution_test") generated 2 warnings
    Finished `test` profile [unoptimized + debuginfo] target(s) in 10.75s
     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

История (1 версия и 0 коментара)

Иван качи първо решение на 29.10.2025 23:17 (преди 1 ден)