I have been busy nowadays. Work of course takes a lot of time. Outside of work, I am still trying to improve my skills. I am still trying to read a lot, write a lot, do a lot. The lack of social media in my life has helped immensely. Except Reddit I suppose, I cannot seem to let go of that one in particular.
Anyways, I started working on the 100 exercises to learn Rust for starters. Today's exercise was factorials without the use of loops. I wrote this little monster below.
fn factorial(n: i32) -> i32 {
let mut sum: i32 = 0;
if n <= 1 {
if sum <= 0 {
return 1;
} else {
return sum;
}
} else {
sum = n * factorial(n - 1);
};
return sum;
}
I am happy with how it came out. It recursively applies the factorial and pumps out the number. Great job me! It is a nice start. Doing these mostly offline and if needed, Rust docs. The exercise itself seems to be self explanatory at most times, aiming for people who already know how to code.
Where to find:
For anyone interested, here's the link: https://rust-exercises.com/100-exercises
Toodles!
Discussion
Comments
New comments are moderated before they appear publicly.
0 approved comments