June 17, 2024 · (updated August 5, 2024) · 4 min · 791 words · Kristian Alexander P
This will be a part of a series on my rust-learning journey. The goal of this post is to document my process of creating a simple program that determine if the number given is a perfect number1. And also list the perfect number until the given number.
This will calculate all the perfect number up to the given number. The return type is a vector, which is a resizeable arrays in rust. It size is unknown at compile time, and it can shrink or grow at any time.
fnmain(){println!("Input a number!\n");letmutinput_number=String::new();io::stdin().read_line(&mutinput_number).expect("failed to read input.");letx: i32=input_number.trim().parse().expect("Not an integer");println!("Your input is: {}",x);ifis_perfect_number(x){println!("{} is a perfect number",x)}else{println!("{} is not a perfect number",x)}println!("Input a number!\n");letmutnumber_range=String::new();io::stdin().read_line(&mutnumber_range).expect("Failed to read input.");lety: i32=number_range.trim().parse().expect("Not an integer");print!("The perfect number from 1 to {} is: ",y);forelementin&perfect_number_list(y){print!("{} ",element);}println!();// for whitespace
}
I’m sure there are packages that simplify this process, but when I’m learning new programming languages I try to stick with the builtins first. But basically it goes like this:
String::new
It creates an empty string.
I don’t actually know if all stdin should be treated as string.
io::stdin
From the std::io module.
.readline
The actual reading. The &mut before the variable input_number means it is a mutable reference to the variable input_number.
.expect
In case the input is not a string.
The conversion from string to integer is done by declaring it into another variable and:
.trim
clear the whitespaces if any.
.parse
parsing it into another type, in this case i32, and lastly,
.expect
Guard it in case it is a different type
The rest of this main function is by using the two functions earlier.
usestd::io;fnis_perfect_number(n: i32)-> bool{letmutsum: i32=1;letmuti: i32=2;whilei*i<=n{ifn%i==0{sum=sum+i+n/i;}i+=1;}ifsum==n&&n!=1{returntrue}else{returnfalse}}fnperfect_number_list(n: i32)-> Vec<i32>{letmutnumbers: Vec<i32>=vec![];fornumin1..n{ifis_perfect_number(num){numbers.push(num);}}returnnumbers;}fnmain(){println!("Input a number!\n");letmutinput_number=String::new();io::stdin().read_line(&mutinput_number).expect("failed to read input.");letx: i32=input_number.trim().parse().expect("Not an integer");println!("Your input is: {}",x);ifis_perfect_number(x){println!("{} is a perfect number",x)}else{println!("{} is not a perfect number",x)}println!("Input a number!\n");letmutnumber_range=String::new();io::stdin().read_line(&mutnumber_range).expect("Failed to read input.");lety: i32=number_range.trim().parse().expect("Not an integer");print!("The perfect number from 1 to {} is: ",y);forelementin&perfect_number_list(y){print!("{} ",element);}println!();// for whitespace
}