LINUX.ORG.RU

История изменений

Исправление rtxtxtrx, (текущая версия) :

И чем оно лучше этого?

use std::thread;
use std::time::Duration;

#[derive(Debug, Clone, Copy)]
enum State {
    Disconnected,
    Connecting,
    Connected,
}

struct Connection {
    state: State,
}

impl Connection {
    fn new() -> Self {
        Connection { state: State::Disconnected }
    }

    fn connect(&mut self) {
        match self.state {
            State::Disconnected => {
                println!("Connecting...");
                self.state = State::Connecting;
                thread::sleep(Duration::from_secs(2)); // Эмуляция подключения
                self.state = State::Connected;
                println!("Connection established.");
            }
            State::Connecting => {
                println!("Already connecting.");
            }
            State::Connected => {
                println!("Already connected.");
            }
        }
    }

    fn ping(&self) {
        match self.state {
            State::Connected => {
                println!("Ping succeeded.");
            }
            _ => {
                println!("Cannot ping from current state.");
            }
        }
    }

    fn drop(&mut self) {
        match self.state {
            State::Connected | State::Connecting => {
                println!("Dropping connection.");
                self.state = State::Disconnected;
            }
            State::Disconnected => {
                println!("Already disconnected.");
            }
        }
    }
}


fn main() {
    let mut connection = Connection::new();

    connection.connect();
    connection.ping();
    connection.drop();
}

Исходная версия rtxtxtrx, :

И чем оно лучше этого?

use std::thread;
use std::time::Duration;

struct Connection {
    state: State,
}

impl Connection {
    fn new() -> Self {
        Connection { state: State::Disconnected }
    }

    fn connect(&mut self) {
        match self.state {
            State::Disconnected => {
                println!("Connecting...");
                self.state = State::Connecting;
                thread::sleep(Duration::from_secs(2)); // Эмуляция подключения
                self.state = State::Connected;
                println!("Connection established.");
            }
            State::Connecting => {
                println!("Already connecting.");
            }
            State::Connected => {
                println!("Already connected.");
            }
        }
    }

    fn ping(&self) {
        match self.state {
            State::Connected => {
                println!("Ping succeeded.");
            }
            _ => {
                println!("Cannot ping from current state.");
            }
        }
    }

    fn drop(&mut self) {
        match self.state {
            State::Connected | State::Connecting => {
                println!("Dropping connection.");
                self.state = State::Disconnected;
            }
            State::Disconnected => {
                println!("Already disconnected.");
            }
        }
    }
}


fn main() {
    let mut connection = Connection::new();

    connection.connect();
    connection.ping();
    connection.drop();
}