📖 Scenario: Imagine you are organizing a small city map. The map has places (like parks, schools) and roads connecting them. Some roads have directions (one-way), and some have distances (weights).
🎯 Goal: You will create a simple graph using Rust to represent places as vertices and roads as edges. You will mark if roads are one-way (directed) or two-way (undirected) and include distances (weights) for each road.
📋 What You'll Learn
Create a vector of vertices called places with exact names
Create a vector of edges called roads with tuples containing start place, end place, direction, and distance
Add a boolean variable is_directed to indicate if the graph is directed
Use a for loop to print each road with its details
Print the final list of roads showing start, end, direction, and distance
💡 Why This Matters
🌍 Real World
Maps, navigation apps, and network diagrams use graphs to represent locations and connections.
💼 Career
Understanding graph basics is essential for software roles involving data structures, algorithms, and system design.
Progress0 / 4 steps
1
Create the list of places (vertices)
Create a vector called places with these exact strings: "Park", "School", "Mall", "Library"
DSA Rust
Hint
Use vec![...] to create a list of place names exactly as given.
2
Create the list of roads (edges) with direction and distance
Create a vector called roads with these exact tuples: ("Park", "School", true, 5), ("School", "Mall", false, 10), ("Mall", "Library", true, 3). Each tuple has start place, end place, is_one_way boolean, and distance as integer.
DSA Rust
Hint
Use a vector of tuples with the exact values and order given.
3
Add a variable to indicate if the graph is directed
Add a boolean variable called is_directed and set it to true.
DSA Rust
Hint
Just create a boolean variable named is_directed and assign true.
4
Print the roads with their details
Use a for loop with variables start, end, one_way, and distance to iterate over roads.iter(). Inside the loop, print each road in this format: "Road from {start} to {end}, one way: {one_way}, distance: {distance}".
DSA Rust
Hint
Use println! inside the loop to print each road's details exactly as shown.
Practice
(1/5)
1. In graph terminology, what is a vertex?
easy
A. A point or node in the graph where edges meet
B. A line connecting two points in the graph
C. A graph that has no edges
D. A graph with weighted edges
Solution
Step 1: Understand the definition of vertex
A vertex is a fundamental unit in a graph representing a point or node.
Step 2: Differentiate vertex from edges and graph types
Edges connect vertices; graphs can be weighted or unweighted, directed or undirected, but vertices are the points themselves.
Final Answer:
A point or node in the graph where edges meet -> Option A
Quick Check:
Vertex = point/node [OK]
Hint: Vertices are points, edges are lines connecting them [OK]
Common Mistakes:
Confusing vertices with edges
Thinking vertex means weighted edge
Mixing vertex with graph type
2. Which of the following is the correct Rust syntax to declare an undirected edge between vertices 1 and 2 using a tuple?
easy
A. let edge = {1, 2};
B. let edge = (1, 2);
C. let edge = [1, 2];
D. let edge = <1, 2>;
Solution
Step 1: Recall Rust tuple syntax
Tuples in Rust are declared with parentheses and comma-separated values, e.g., (1, 2).
Step 2: Check other options for correctness
Square brackets [] are for arrays, curly braces {} are for blocks or sets (not in Rust), angle brackets <> are not used for tuples.
Final Answer:
let edge = (1, 2); -> Option B
Quick Check:
Tuple syntax uses parentheses ( ) [OK]
Hint: Tuples use parentheses ( ), arrays use brackets [ ] [OK]
Common Mistakes:
Using square brackets for tuples
Using curly braces instead of parentheses
Confusing angle brackets with tuple syntax
3. What is the output of the following Rust code that creates a weighted directed edge and prints its components?
struct Edge {
from: usize,
to: usize,
weight: i32,
}
fn main() {
let e = Edge { from: 3, to: 5, weight: 10 };
println!("{} -> {} with weight {}", e.from, e.to, e.weight);
}
medium
A. 3 -> 5 with weight 0
B. 5 -> 3 with weight 10
C. 3 -> 5 with weight 10
D. Compilation error due to missing semicolon
Solution
Step 1: Understand struct initialization
The struct Edge is initialized with from=3, to=5, weight=10 correctly.
Step 2: Analyze the println! macro output
The print statement outputs the from, to, and weight fields in order: "3 -> 5 with weight 10".
Final Answer:
3 -> 5 with weight 10 -> Option C
Quick Check:
Prints from-to-weight as given [OK]
Hint: Struct fields print as assigned, watch order in println! [OK]
Common Mistakes:
Swapping from and to values
Assuming default weight 0
Thinking missing semicolon causes error here
4. Identify the error in this Rust code snippet that tries to create an undirected weighted edge:
struct Edge {
vertex1: usize,
vertex2: usize,
weight: i32,
}
fn main() {
let e = Edge { vertex1: 1, vertex2: 2 };
println!("Edge between {} and {} with weight {}", e.vertex1, e.vertex2, e.weight);
}
medium
A. Missing weight field initialization causes a compile error
B. vertex1 and vertex2 should be swapped
C. println! macro syntax is incorrect
D. Edge struct should not have weight for undirected edges
Solution
Step 1: Check struct initialization
The Edge struct requires vertex1, vertex2, and weight fields. Weight is missing in initialization.
Rust requires all fields to be initialized; missing weight causes compile-time error.
Final Answer:
Missing weight field initialization causes a compile error -> Option A
Quick Check:
All struct fields must be initialized [OK]
Hint: All struct fields must be given values when creating instance [OK]
Common Mistakes:
Assuming missing fields default to zero
Thinking vertex order matters for undirected edges
Believing println! syntax is wrong here
5. You want to represent a weighted undirected graph in Rust using an adjacency list. Which data structure is the best choice to store edges with weights efficiently?
hard
A. HashMap storing only vertex connections without weights
B. Vec storing neighbors only, ignoring weights
C. Vec<(usize, usize, i32)> storing all edges as tuples without adjacency
D. Vec> where each inner Vec holds (neighbor_vertex, weight)
Solution
Step 1: Understand adjacency list for weighted graphs
Weighted graphs need to store neighbors and weights; a Vec of Vec of tuples (neighbor, weight) fits well.
Step 2: Evaluate other options
HashMap without weights loses weight info; flat Vec of tuples lacks adjacency structure; Vec of usize ignores weights.
Final Answer:
Vec> where each inner Vec holds (neighbor_vertex, weight) -> Option D
Quick Check:
Adjacency list with (neighbor, weight) tuples is best [OK]
Hint: Use adjacency list with (neighbor, weight) tuples for weighted graphs [OK]
Common Mistakes:
Ignoring weights in adjacency list
Using flat edge list without adjacency
Using HashMap without weight info
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.