Logo

Programming-Idioms

History of Idiom 76 > diff from v12 to v13

Edit summary for version 13 by :

Version 12

2015-08-21, 23:36:29

Version 13

2015-08-21, 23:37:01

Idiom #76 Binary digits from an integer

Create the string s of integer x written in base 2.

E.g. 13 -> "1101"

Idiom #76 Binary digits from an integer

Create the string s of integer x written in base 2.

E.g. 13 -> "1101"

Code
let s = format!("{:b}", x);
Code
let s = format!("{:b}", x);
Comments bubble
Formatting allows you to choose another representation (e.g., `b` for binary, `X` for hex in upper case and `_?` for debug output).
Comments bubble
Formatting lets you choose another representation (e.g., `b` for binary, `X` for hex in upper case and `_?` for debug output).
Doc URL
http://doc.rust-lang.org/std/fmt/index.html
Doc URL
http://doc.rust-lang.org/std/fmt/index.html
Demo URL
https://play.rust-lang.org/?code=fn%20main()%20%7B%0A%20%20%20%20let%20x%20%3D%2013%3B%0A%20%20%20%20let%20s%20%3D%20format!(%22%7B%3Ab%7D%22%2C%20x)%3B%0A%20%20%20%20%0A%20%20%20%20println!(%22%7B%7D%22%2C%20s)%3B%0A%7D&version=stable
Demo URL
https://play.rust-lang.org/?code=fn%20main()%20%7B%0A%20%20%20%20let%20x%20%3D%2013%3B%0A%20%20%20%20let%20s%20%3D%20format!(%22%7B%3Ab%7D%22%2C%20x)%3B%0A%20%20%20%20%0A%20%20%20%20println!(%22%7B%7D%22%2C%20s)%3B%0A%7D&version=stable