Create a function to convert a decimal number to binary using Rust.
Create a function to convert a decimal number to binary using Rust.
291
22-Jun-2023
Aryan Kumar
23-Jun-2023Sure, here is a Rust function to convert a decimal number to binary using Rust:
Rust
This function first defines a function called decimal_to_binary that takes an i32 number as input and returns a String. The function first initializes a variable called binary_string to an empty string. Then, the function iterates through the number, dividing the number by 2 and adding the remainder to the binary_string. Finally, the function reverses the binary_string and returns it.
The main function of the program then calls the decimal_to_binary function with the value 10 as input. The result of the function call is then printed to the console.
To run the program, you can save it as a file called decimal_to_binary.rs and then compile it using the following command:
Code snippet
Once the program is compiled, you can run it using the following command:
Code snippet
This will print the following output to the console:
Code snippet