MCPcopy Create free account
hub / github.com/QMHTMY/RustBook / divide_by_two

Function divide_by_two

code/chapter03/divide_by_two.rs:31–50  ·  view source on GitHub ↗
(mut dec_num: u32)

Source from the content-addressed store, hash-verified

29}
30
31fn 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
52fn main() {
53 let bin_str: String = divide_by_two(10);

Callers 1

mainFunction · 0.85

Calls 4

pushMethod · 0.45
to_stringMethod · 0.45
is_emptyMethod · 0.45
popMethod · 0.45

Tested by

no test coverage detected