Unlock Your Python Backend Career: Build 30 Projects in 30 Days. Join now for just $54

Chapter 3: Building with Rust

Chapter 3: Building with Rust

Final Rust Code Listing

Final Rust Code Listing

The final code listing looks like this:

use std::cmp::Ordering;
use std::convert::TryInto;
use std::io;


fn get_input(var: &mut String) -> i32 {
    io::stdin()
        .read_line(var)
        .expect("Failed to read line");

    var.chars()
        .count()
        .try_into()
        .expect("Length of name is too large!")
}

fn calculate_compatibility(name1_count: i32, name2_count: i32) -> f32 {
    match name1_count.cmp(&name2_count) {
        Ordering::Less => {
            ((name1_count as f32 / name2_count as f32) as f32 * 100.0) as f32
        },
        Ordering::Greater => {
            ((name2_count as f32 / name1_count as f32) as f32 * 100.0) as f32
        },
        Ordering::Equal => {
            50.0
        }
    }
}


fn main() {
    let mut name1 = String::new();
    let mut name2 = String::new();

    println!("Enter first person name: ");
    let name1_count = get_input(&mut name1);

    println!("Enter second person name: ");
    let name2_count = get_input(&mut name2);

    let compat_value = calculate_compatibility(name1_count, name2_count).floor();
    
    println!("Love compatibility between {} and {} is {}%", name1.trim(), name2.trim(), compat_value);
}

The final code can be gotten here, a repo of mine dedicated to learning Rust and its paradigms.

More Resources

  1. Official Documentation

  2. Introduction to Rust

  3. RUST Beginners Tips & Tricks Guide 2020

  4. Learing Rust

  5. Ultimate Rust Crash Course is among the best courses for Rust programming language. The instructor explaining complex terms with ease and aid the understanding of the language.

  6. Learn Rust by Building Real Applications takes you to build real-life applications using the Rust programming language. If you’re looking at getting your hands duty with projects, this course is definitely for you.

Whenever you're ready

There are 4 ways we can help you become a great backend engineer:

The MB Platform

Join 1000+ backend engineers learning backend engineering. Build real-world backend projects, learn from expert-vetted courses and roadmaps, track your learnings and set schedules, and solve backend engineering tasks, exercises, and challenges.

The MB Academy

The “MB Academy” is a 6-month intensive Advanced Backend Engineering BootCamp to produce great backend engineers.

Join Backend Weekly

If you like post like this, you will absolutely enjoy our exclusive weekly newsletter, Sharing exclusive backend engineering resources to help you become a great Backend Engineer.

Get Backend Jobs

Find over 2,000+ Tailored International Remote Backend Jobs or Reach 50,000+ backend engineers on the #1 Backend Engineering Job Board