Exploring petgraph!

// A rudimentary MVP 

use petgraph::{graph::Graph, dot::{Dot, Config}, Undirected};

#[derive(Debug)]
enum Popularity {
    T10,
    T100,
    T500,
    Other
}

#[derive(Debug)]
struct Standard <'a> {
    name: &'a str,
    ticker: &'a str,
    weekly_tracker: Vec<i32>, 
    popularity: Popularity
}

impl<'a> Standard <'a> {
    fn new(name: &'a str, ticker: &'a str, weekly_tracker: Vec<i32>, popularity: Popularity) -> Self {
        Standard{name, ticker, weekly_tracker, popularity}
    }
}

#[derive(Debug)]
struct Relationship {
    strength: u64
}

impl Relationship {
    fn new(strength: u64) -> Self {
        Relationship{strength}
    }
}

fn main() {
    
    let mut graph: Graph<Standard, Relationship, Undirected> = Graph::new_undirected();
    let test_company_1 = Standard::new(
        "test", 
        "t", 
        vec![127, 129, 125, 129, 140, 138, 135], 
        Popularity::T100
    );
    
    let test_company_2 = Standard::new(
        "boom", 
        "b", 
        vec![166, 164, 170, 172, 188, 162, 180], 
        Popularity::T10
    );
    
    let test_company_3 = Standard::new(
        "cat", 
        "c", 
        vec![54, 53, 51, 60, 55, 87, 75], 
        Popularity::T500
    );
    
    let i1 = graph.add_node(test_company_1);
    let i2 = graph.add_node(test_company_2);
    let i3 = graph.add_node(test_company_3);
    
    let onetwo_edge = Relationship::new(50);
    let twothree_edge = Relationship::new(70);
    let onethree_edge = Relationship::new(65);
    
    graph.add_edge(i1, i2, onetwo_edge);
    graph.add_edge(i2, i3, twothree_edge);
    graph.add_edge(i1, i3, onethree_edge);
    
    println!("{:?}", Dot::with_config(&graph, &[]));
}

(Playground)

Output:

graph {
    0 [ label = "Standard { name: \"test\", ticker: \"t\", weekly_tracker: [127, 129, 125, 129, 140, 138, 135], popularity: T100 }" ]
    1 [ label = "Standard { name: \"boom\", ticker: \"b\", weekly_tracker: [166, 164, 170, 172, 188, 162, 180], popularity: T10 }" ]
    2 [ label = "Standard { name: \"cat\", ticker: \"c\", weekly_tracker: [54, 53, 51, 60, 55, 87, 75], popularity: T500 }" ]
    0 -- 1 [ label = "Relationship { strength: 50 }" ]
    1 -- 2 [ label = "Relationship { strength: 70 }" ]
    0 -- 2 [ label = "Relationship { strength: 65 }" ]
}


Errors:

   Compiling playground v0.0.1 (/playground)
warning: unused import: `Config`
 --> src/main.rs:3:41
  |
3 | use petgraph::{graph::Graph, dot::{Dot, Config}, Undirected};
  |                                         ^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

warning: variant `Other` is never constructed
  --> src/main.rs:10:5
   |
6  | enum Popularity {
   |      ---------- variant in this enum
...
10 |     Other
   |     ^^^^^
   |
   = note: `Popularity` 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 `name`, `ticker`, `weekly_tracker`, and `popularity` are never read
  --> src/main.rs:15:5
   |
14 | struct Standard <'a> {
   |        -------- fields in this struct
15 |     name: &'a str,
   |     ^^^^
16 |     ticker: &'a str,
   |     ^^^^^^
17 |     weekly_tracker: Vec<i32>, 
   |     ^^^^^^^^^^^^^^
18 |     popularity: Popularity
   |     ^^^^^^^^^^
   |
   = note: `Standard` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis

warning: field `strength` is never read
  --> src/main.rs:29:5
   |
28 | struct Relationship {
   |        ------------ field in this struct
29 |     strength: u64
   |     ^^^^^^^^
   |
   = note: `Relationship` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis

warning: `playground` (bin "playground") generated 4 warnings (run `cargo fix --bin "playground"` to apply 1 suggestion)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.82s
     Running `target/debug/playground`

Heya, if your question is "why do I get these warnings when those fields are clearly printed", you've run into this (I think): Unactionable "field is never read" warning for printed structs with the Debug derive · Issue #88900 · rust-lang/rust · GitHub

Rust doesn't consider Debug usage as actual usage, so once you start reading those fields elsewhere in the code, the warnings should go away.

1 Like

Hey I really appreciate it!! I'm new to Rust. I'm more posting my work as I go along and progress to the intermediate stages. Thank you for the feedback!! If there are any ever optimizations I can do for my code, please let me know. I'm a big fan of this forum. :slight_smile:

1 Like