To find the nth triangular number in Rust, we can use the following formula:
main.rs21 chars2 lines
where Tn
is the nth triangular number.
We can use this formula in a Rust function as follows:
main.rs64 chars4 lines
This function takes an unsigned 32-bit integer n
as input and returns an unsigned 32-bit integer which is the nth triangular number.
We can test this function using Rust's built-in test framework as follows:
main.rs349 chars14 lines
This test module checks if the nth_triangular_number()
function returns the correct values for the input values 1 to 5.
gistlibby LogSnag