(mut dec_num: u32)
| 29 | } |
| 30 | |
| 31 | fn divide_by_two(mut dec_num: u32) -> String { |
| 32 | // 用栈来保存余数 rem |
| 33 | let mut rem_stack = Stack::new(); |
| 34 | |
| 35 | // 余数 rem 入栈 |
| 36 | while dec_num > 0 { |
| 37 | let rem = dec_num % 2; |
| 38 | rem_stack.push(rem); |
| 39 | dec_num /= 2; |
| 40 | } |
| 41 | |
| 42 | // 栈中元素出栈组成字符串 |
| 43 | let mut bin_str = "".to_string(); |
| 44 | while !rem_stack.is_empty() { |
| 45 | let rem = rem_stack.pop().unwrap().to_string(); |
| 46 | bin_str += &rem; |
| 47 | } |
| 48 | |
| 49 | bin_str |
| 50 | } |
| 51 | |
| 52 | fn main() { |
| 53 | let bin_str: String = divide_by_two(10); |