Bad design for Rust program with lifetime and multiple references

I am very new to the Rust language so please bear with me.

I am implementing a graph data structure in Rust. For each node (and also edges – omitted from this snippet) in the graph I have a struct “Algorithm” (perhaps poorly named) that performs some node and edge based logic. Algorithms require knowledge of the graph connectivity and so cannot belong to a node or edge themselves. They are based on trait polymorphism, although I plan to refactor this to enum polymorphism later. Lastly, I have a solver that works by iterating the nodes of the graph and creating an algorithm for it. Please note, the example is heavily distilled down.

However, I am having two problems. The first is due to the lifetime of the Boxed struct within the “algorithms” vector.

error: lifetime may not live long enough
  --> main.rs:67:29
   |
51 |   impl<'a> Solver<'a> {
   |        -- lifetime `'a` defined here
...
63 |       pub fn solve(&mut self) {
   |                    - let's call the lifetime of this reference `'1`
...
67 |               let algorithm = Box::new(MyAlgorithm {
   |  _____________________________^
68 | |                 graph: &mut self.graph,
69 | |             });
   | |______________^ assignment requires that `'1` must outlive `'a`

I have tried my best to indicate to the compiler that the lifetime of the Algorithm struct should be kept within the lifetime of the Solver struct, but this hasn’t worked.

Upon completion of its job, an Algorithm will need to update the nodes and edges of the graph, and so it requires a mutable reference to the graph object. However, this of course is incorrect due to the second mutable borrow.

cannot borrow `self.graph` as mutable more than once at a time
second mutable borrow occurs here

Below is the code.

struct Node {
    // Define the Node struct...
}

impl Node {
    // Implement methods for Node...
}

struct Graph {
    nodes: Vec<Node>,
}

impl Graph {
    fn new() -> Self {
        Graph {
            nodes: Vec::new(),
            // Initialize other fields...
        }
    }
}

trait BaseAlgorithm<'a> {
    // Define the trait methods...
}

// Create a MyAlgorithm struct that takes a mutable reference to the graph
struct MyAlgorithm<'a> {
    graph: &'a mut Graph,
}

impl<'a> BaseAlgorithm<'a> for MyAlgorithm<'a> {
    // Implement the trait methods for MyAlgorithm...
}

struct Solver<'a> {
    algorithms: Vec<Box<dyn BaseAlgorithm<'a> + 'a>>,
    graph: Graph,
}

impl<'a> Solver<'a> {
    fn new() -> Self {
        Solver {
            algorithms: Vec::new(),
            graph: Graph::new(),
        }
    }

    fn add_algorithm(&mut self, algorithm: Box<dyn BaseAlgorithm<'a> + 'a>) {
        self.algorithms.push(algorithm);
    }

    pub fn solve(&mut self) {
        // Loop over the nodes vector within Graph
        for node in &mut self.graph.nodes {
            // add an algorithm with a mutable reference to Graph
            let algorithm = Box::new(MyAlgorithm {
                graph: &mut self.graph,
            });
            self.add_algorithm(algorithm);
        }
    }
}

fn main() {
    
    let mut solver = Solver::new();

    solver.solve();

}

If the design can be fixed, then I would greatly appreciate any help achieving that. However, if the design cannot be fixed, is there an idiomatic Rust pattern for achieving the requirements of this logic?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật