Skip to content

Convert C++ code to Rust #4

@ugreg

Description

@ugreg

Tasks

  1. Port the existing C++ code to Rust
  2. Document how to setup Rust and run the new code
#include <iostream>
#include <vector>
#include <string>

const std::string RED = "\033[91m";
const std::string YELLOW = "\033[93m";
const std::string BLUE = "\033[94m";
const std::string PINK = "\033[95m";
const std::string GREY = "\033[90m";
const std::string RESET = "\033[0m";

const std::string filledSquare = "\u25A0";

void drawColoredBar(int percentage, const std::string& color) {
    const int maxBarWidth = 50;
    int filledWidth = static_cast<int>((static_cast<double>(percentage) / 100) * maxBarWidth);

    std::cout << color;
    for (int i = 0; i < filledWidth; ++i) {
        std::cout << filledSquare;
    }
    std::cout << GREY;
    for (int i = filledWidth; i < maxBarWidth; ++i) {
        std::cout << filledSquare;
    }
    std::cout << RESET << " (" << percentage << "%)" << std::endl;
}

int main() {
    std::vector<int> percentages = {52, 48, 32, 64};
    std::vector<std::string> colors = {RED, YELLOW, BLUE, PINK};

    for (size_t i = 0; i < percentages.size(); ++i) {
        drawColoredBar(percentages[i], colors[i]);
    }

    return 0;
}

Acceptance criteria
The code must compile and it must produce the same output as seen here:
Image. Create a pull request to the main branch with the working code.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions