Решение на упр.02 задача 1 от Петя Петрова

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

Към профила на Петя Петрова

Резултати

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

Код

#[derive(Debug)]
struct SensorData {
temperature: f64,
radiation_level: u32,
}
#[derive(Debug)]
struct Probe {
name: String,
battery_percentage: u8,
last_reading: SensorData,
status: String,
}
fn create_sensor_data(temperature: f64, radiation_level: u32) -> SensorData {
SensorData {
temperature,
radiation_level,
}
}
fn create_probe(name: &str, battery_percentage: u8, sensor_data: SensorData) -> Probe {
Probe {
name: name.to_string(),
battery_percentage,
last_reading: sensor_data,
status: "Active".to_string(),
}
}
fn diagnostic_check(probe: &Probe) {
if probe.battery_percentage < 20 {
println!("WARNING: Probe '{}' has critically low battery ({}%)!", probe.name, probe.battery_percentage);
} else {
println!("Diagnostic report for probe: {:?}", probe);
}
}
fn recharge_probe(probe: &mut Probe, amount: u8) {
let new_battery = (probe.battery_percentage as u16) + (amount as u16);
if new_battery > 100 {
probe.battery_percentage = 100;
} else {
probe.battery_percentage = new_battery as u8;
}
println!("Probe '{}' has been recharged. Current battery: {}%", probe.name, probe.battery_percentage);
}
fn update_status(probe: &mut Probe, new_status: &str) {
probe.status = new_status.to_string();
println!("Probe '{}' status updated to: '{}'", probe.name, probe.status);
}
fn analyze_critical_sensor(sensor: SensorData) {
println!("Analyzing sensor data (temperature: {}°C, radiation: {})...", sensor.temperature, sensor.radiation_level);
if sensor.radiation_level > 100 || sensor.temperature > 80.0 || sensor.temperature < -50.0 {
println!("ALARM: Extreme planetary conditions detected!");
} else {
println!("Sensor metrics are within normal ranges.");
}
// The function ends here, and memory for the passed SensorData is automatically freed.
}
fn main() {
let initial_sensor = create_sensor_data(45.5, 120);
let mut probe1 = create_probe("Voyager-X", 15, initial_sensor);
println!("--- INITIAL DIAGNOSTIC ---");
diagnostic_check(&probe1);
println!("\n--- STATE MODIFICATION ---");
recharge_probe(&mut probe1, 50); // Battery becomes 65%
update_status(&mut probe1, "Exploring");
println!("\n--- AFTER MODIFICATION ---");
diagnostic_check(&probe1); // Now prints the full debug representation
println!("\n--- DEMONSTRATION OF PARTIAL MOVE ---");
let reading = probe1.last_reading;
analyze_critical_sensor(reading);
/*
* Comment on Partial Move:
* After extracting `probe1.last_reading`, that specific field has been moved out
* of the `probe1` structure. The probe instance `probe1` itself becomes partially
* uninitialized/invalid for full operations (e.g., we can no longer print the entire
* probe using `{:?}` or pass it whole by value), because the compiler knows one of
* its fields is missing. However, fields like `probe1.battery_percentage` or
* `probe1.status` can still be accessed or modified, as long as we don't attempt
* to access the moved `last_reading` field.
*/
}

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

Петя качи първо решение на 15.10.2026 14:21 (преди около 2 месеца)