rust square root
To calculate the square root of a number in Rust, you can use the sqrt
function provided by the f64
module. Here's an example:
fn main() {
let num = 16.0;
let square_root = num.sqrt();
println!("The square root of {} is {}", num, square_root);
}
In this example, we declare a variable num
and assign it the value 16.0
. We then call the sqrt
function on num
to calculate its square root, and store the result in the square_root
variable. Finally, we use println!
to display the original number and its square root.
The output of this program will be:
The square root of 16.0 is 4
Note that the sqrt
function is specifically for floating-point numbers (f64
). If you want to calculate the square root of an integer, you can convert it to a floating-point number first using the as
keyword.