Job Sequencing in Rust

Published on 15 April 2023 (Updated: 08 May 2023)

Welcome to the Job Sequencing in Rust page! Here, you'll find the source code for this program as well as a description of how the program works.

Current Solution

use std::env::args;
use std::process::exit;
use std::str::FromStr;
use std::cmp::Ordering;

fn usage() -> ! {
    println!("Usage: please provide a list of profits and a list of deadlines");

fn parse_int<T: FromStr>(s: &str) -> Result<T, <T as FromStr>::Err> {

fn parse_int_list<T: FromStr>(s: &str) -> Result<Vec<T>, <T as FromStr>::Err> {
        .collect::<Result<Vec<T>, <T as FromStr>::Err>>()

#[derive(Debug, Ord, Eq)]
struct JobInfo {
    job_id: usize,
    profit: i32,
    deadline: usize,

impl JobInfo {
    fn new(job_id: usize, profit: i32, deadline: usize) -> Self {
        Self {job_id: job_id, profit: profit, deadline: deadline}

impl PartialOrd for JobInfo {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        // Reverse order of compare so that it is in descending order by profit
        // then deadline
        match self.profit != other.profit {
            true => Some(other.profit.cmp(&self.profit)),
            false => Some(other.deadline.cmp(&self.deadline)),

impl PartialEq for JobInfo {
    fn eq(&self, other: &Self) -> bool {
        self.profit == other.profit && self.deadline == other.deadline

// Job sequencing with deadlines
// Source:
fn job_sequencing(profits: &Vec<i32>, deadlines: &Vec<i32>) -> Vec<JobInfo> {
    // Set up job details
    let mut jobs: Vec<JobInfo> = profits.iter()
        .map(|(n, (&p, &d))| JobInfo::new(n + 1, p, d as usize))

    // Get longest deadline
    let longest_deadline: i32 = *deadlines.iter()

    // Initialize job slots
    let mut slots: Vec<JobInfo> = (0..longest_deadline)
        .map(|_| JobInfo::new(0, 0, 0))

    // Sort jobs by profit then deadline

    // For each job, see if there is available slot at or before the deadline.
    // If so, store this job in that slot
    for job in jobs {
        for j in (0..job.deadline).rev() {
            if slots[j].job_id < 1 {
                slots[j] = job;


fn get_total_profit(jobs: &Vec<JobInfo>) -> i32 {
        .map(|x| x.profit)

fn main() {
    let mut args = args().skip(1);

    // Convert 1st command-line argument to list of integers
    let mut profits: Vec<i32> = args
        .and_then(|s| parse_int_list(&s).ok())
        .unwrap_or_else(|| usage());

    // Convert 2nd command-line argument to list of integers
    let mut deadlines: Vec<i32> = args
        .and_then(|s| parse_int_list(&s).ok())
        .unwrap_or_else(|| usage());

    // Exit if profits not same length as deadlines
    if profits.len() != deadlines.len() {

    // Get job sequence
    let jobs = job_sequencing(&profits, &deadlines);

    // Get total profit and display
    println!("{}", get_total_profit(&jobs));

Job Sequencing in Rust was written by:

If you see anything you'd like to change or update, please consider contributing.

How to Implement the Solution

No 'How to Implement the Solution' section available. Please consider contributing.

How to Run the Solution

No 'How to Run the Solution' section available. Please consider contributing.