Решение на упр.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: temperature,
radiation_level: radiation_level,
}
}
fn create_prob(name: &str, battery_percentage: u8, last_reading: SensorData) -> Probe {
Probe {
name: name.to_string(),
battery_percentage: battery_percentage,
last_reading: last_reading,
status: "not in exploitation".to_string(),
}
}
fn diagnostic_check(probe: &Probe) {
if probe.battery_percentage < 20 {
println!("Warning! Battery is only at {}%", probe.battery_percentage);
} else {
println!("{:?}", probe);
}
}
fn recharge_probe(probe: &mut Probe, amount: u8) {
if probe.battery_percentage + amount > 100 {
probe.battery_percentage = 100;
}
else{
probe.battery_percentage += amount
}
}
fn update_status(probe: &mut Probe, new_status: &str){
probe.status = new_status.to_string();
}
fn analyze_critical_sensor(sensor: SensorData){
if sensor.radiation_level > 100 || sensor.temperature > 67.42 {
println!("Warning! {:?}", sensor);
}
}
fn main() {
let sensor1 = create_sensor_data(23.43, 110);
let mut probe1 = create_prob("humongous_prob", 21, sensor1);
diagnostic_check(&probe1);
update_status(&mut probe1, "Exploring");
let reading = probe1.last_reading;
analyze_critical_sensor(reading);
// recharge_probe(&mut probe1, 12);
// probe1.last_reading вече няма права над сензора, така че е неизползваем
probe1.battery_percentage += 5;
// тук понеже не подавам probe1 на функция, гарантирам, че няма да се достъпва probe1.last_reading
}

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

Наум качи първо решение на 21.10.2026 17:25 (преди 6 дена)