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
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.
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.