├── .gitignore ├── .travis.yml ├── Cargo.lock ├── Cargo.toml ├── LICENSE ├── README.md ├── index.html ├── reset.css ├── src └── main.rs └── styles.css /.gitignore: -------------------------------------------------------------------------------- 1 | target 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: rust 2 | branches: 3 | only: 4 | - gh-pages 5 | notifications: 6 | email: 7 | recipients: 8 | - carol.nichols@gmail.com 9 | on_success: never 10 | on_failure: change 11 | -------------------------------------------------------------------------------- /Cargo.lock: -------------------------------------------------------------------------------- 1 | [root] 2 | name = "rust-conversion-reference" 3 | version = "0.0.3" 4 | 5 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | 3 | name = "rust-conversion-reference" 4 | version = "0.0.3" 5 | authors = ["Carol (Nichols || Goulding) "] 6 | description = "How to convert any thing to any other thing in rust." 7 | repository = "https://github.com/carols10cents/rust-conversion-reference" 8 | readme = "README.md" 9 | license = "MIT" -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Carol (Nichols || Goulding) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Rust conversion reference 2 | 3 | [![Build Status](https://travis-ci.org/carols10cents/rust-conversion-reference.svg?branch=gh-pages)](https://travis-ci.org/carols10cents/rust-conversion-reference) 4 | 5 | Have a `uint`? Want a `&str`? [Here's how!](http://carols10cents.github.io/rust-conversion-reference) 6 | 7 | **Valid with rustc 1.0.0-nightly (f4f10dba2 2015-01-17 20:31:08 +0000)** 8 | 9 | ## Contributors 10 | 11 | * [Carol (Nichols || Goulding)](https://github.com/carols10cents) 12 | * [Tim Parenti](https://github.com/timparenti) 13 | * [Reviewers on Reddit](http://www.reddit.com/r/rust/comments/2qfbog/merry_rustmas_a_rust_conversion_reference_for_you/) 14 | * You???? :heart: 15 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Rust Conversion Reference 4 | 5 | 6 | 7 | 8 |

Rust Conversion Reference

9 | Valid with rustc 1.0.0-nightly (f4f10dba2 2015-01-17 20:31:08 +0000) 10 | 11 |
12 |

Primitives

13 |

There are many ways to convert these types between each other; these are the most straightforward, least surprising ones I've found. Please see the happy path test cases for what I expect their behavior to be.

14 |

Some of these conversions, the ones that use .unwrap(), may panic if the conversion can't be performed on the input you supply!

15 | 16 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 |
Want x to be ▶
Have an x of type ▼
i32u32Stringf64
i32n/ax as u32x.to_string()x as f64
u32x as i32n/ax.to_string()x as f64
String*x.parse().unwrap()x.parse().unwrap()n/ax.parse().unwrap()
f64x as i32x as u32x.to_string()n/a
62 | 63 |

* Observant readers will notice that yes, the code for converting Strings to i32, u32, AND f64 is all the same — how can that be??? Type inference! If Rust can't infer the type that you're trying to get from the context in which you're using it, you can give it a hint by doing, for example, x.parse::<i32>().unwrap(), but that usually isn't necessary.

64 |
65 | 66 |
67 |

Options

68 |

If you have a variable x, and you get the message "expected `type`, found `core::option::Option<type>`", you can use the following code with the following consequences. There is more detail and more ways to handle Options in the Error Handling section of the Rust Programming Language book.

69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 |
CodeConsequence when you get None
x.unwrap_or(put_default_value_here)You will get the value you put where it says put_default_value_here.
x.expect("My Custom Error Message")Panic with a custom error message
x.unwrap()Panic (not recommended in production code, handle your error cases!)
91 |
92 | 93 |
94 |

&str/String/collections::string::String

95 |

Strings are... special. It's a long story that the Strings section of the Rust Programming Language book goes into.

96 |

I've pulled the string types out into a separate table because it's not possible to get an &str without going through String, so it would add a lot of redundancy. Also, when converting between string slices (&str) and in-memory strings (String), you will need to consider the lifetime you intend these to have.

97 |

Also note that collections::string is re-exported as std::string, so if you get an error message saying "expected collections::string::String", that's the same as if the error message said "expected String".

98 |

So. Here are the ways to convert between these when the lifetimes can be inferred:

99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 |
Want x to be ▶
Have an x of type ▼
String&str
Stringn/a&*x
&strx.to_string()n/a
116 |
117 | 118 |
119 |

Vec<T>/&[T]/Box<[T]>

120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 |
Want x to be ▶
Have an x of type ▼
Vec<T>&[T]Box<[T]>
Vec<T>n/a&x[..]x.into_boxed_slice()
&[T]x.to_vec()n/aBox::new(*x)
Box<[T]>x.into_vec()&*xn/a
146 |

Bare [T], referring to some number of T in contiguous memory, are rarely useful. Usually, you want a "borrowed slice", &[T], which consists of a pointer to that memory and a count of the number of T present. If you somehow have a [T] and need &[T] instead, simply use &x.

147 |
148 | 149 |
150 |

I hope you find this helpful! <3

151 |

I'm just a person who makes mistakes just like you, and Rust changes frequently, so if something on this page isn't right, please make a correction!

152 |

This is not an official Rust project.

153 |
154 | Fork me on GitHub 155 | 156 | 157 | -------------------------------------------------------------------------------- /reset.css: -------------------------------------------------------------------------------- 1 | html, body, body div, span, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, abbr, address, cite, code, del, dfn, em, img, ins, kbd, q, samp, small, strong, sub, sup, var, b, i, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td, article, aside, figure, footer, header, menu, nav, section, time, mark, audio, video, details, summary { 2 | margin: 0; 3 | padding: 0; 4 | border: 0; 5 | font-size: 100%; 6 | font-weight: normal; 7 | vertical-align: baseline; 8 | background: transparent; 9 | } 10 | 11 | article, aside, figure, footer, header, nav, section, details, summary {display: block;} 12 | 13 | /* Handle box-sizing while better addressing child elements: 14 | http://css-tricks.com/inheriting-box-sizing-probably-slightly-better-best-practice/ */ 15 | html { 16 | box-sizing: border-box; 17 | } 18 | 19 | *, 20 | *:before, 21 | *:after { 22 | box-sizing: inherit; 23 | } 24 | 25 | /* force a vertical scrollbar to prevent a jumpy page */ 26 | html {overflow-y: scroll;} 27 | 28 | a {margin: 0; padding: 0; font-size: 100%; vertical-align: baseline; background: transparent;} 29 | 30 | /* tables still need cellspacing="0" in the markup */ 31 | table {border-collapse: collapse; border-spacing: 0;} 32 | th {font-weight: bold; vertical-align: bottom;} 33 | td {font-weight: normal; vertical-align: top;} 34 | 35 | 36 | table {font-size: inherit; font: 100%;} 37 | 38 | strong {font-weight: bold;} 39 | 40 | td, td img {vertical-align: top;} 41 | 42 | /* let's clear some floats */ 43 | .clearfix:before, .clearfix:after { content: "\0020"; display: block; height: 0; overflow: hidden; } 44 | .clearfix:after { clear: both; } 45 | .clearfix { zoom: 1; } 46 | -------------------------------------------------------------------------------- /src/main.rs: -------------------------------------------------------------------------------- 1 | // This should fail to compile if any of these code examples are wrong. 2 | 3 | #[allow(dead_code)] 4 | #[allow(unused_variables)] 5 | fn main() { 6 | // Doing the string conversions all in main so that the lifetimes can be inferred. 7 | 8 | let s = "I am a &str."; 9 | 10 | // &str => String 11 | let st: String = s.to_string(); 12 | 13 | // String => &str 14 | let str: &str = &st; 15 | } 16 | 17 | // i32 --------------------------------------------------------------- 18 | #[allow(dead_code)] 19 | fn i32_to_u32(x: i32) -> u32 { 20 | x as u32 21 | } 22 | 23 | #[test] 24 | fn test_i32_to_u32_happy() { 25 | let x = 3i32; 26 | let y = 3u32; 27 | assert_eq!(y, i32_to_u32(x)); 28 | } 29 | 30 | #[allow(dead_code)] 31 | fn i32_to_string(x: i32) -> String { 32 | x.to_string() 33 | } 34 | 35 | #[test] 36 | fn test_i32_to_string_happy() { 37 | let x = 3i32; 38 | let y = "3"; 39 | assert_eq!(y, i32_to_string(x)); 40 | } 41 | 42 | #[allow(dead_code)] 43 | fn i32_to_f64(x: i32) -> f64 { 44 | x as f64 45 | } 46 | 47 | #[test] 48 | fn test_i32_to_f64_happy() { 49 | let x = 3i32; 50 | let y = 3.0f64; 51 | assert_eq!(y, i32_to_f64(x)); 52 | } 53 | 54 | // u32 --------------------------------------------------------------- 55 | 56 | #[allow(dead_code)] 57 | fn u32_to_i32(x: u32) -> i32 { 58 | x as i32 59 | } 60 | 61 | #[test] 62 | fn test_u32_to_i32_happy() { 63 | let x = 3u32; 64 | let y = 3i32; 65 | assert_eq!(y, u32_to_i32(x)); 66 | } 67 | 68 | #[allow(dead_code)] 69 | fn u32_to_string(x: u32) -> String { 70 | x.to_string() 71 | } 72 | 73 | #[test] 74 | fn test_u32_to_string_happy() { 75 | let x = 3u32; 76 | let y = "3"; 77 | assert_eq!(y, u32_to_string(x)); 78 | } 79 | 80 | #[allow(dead_code)] 81 | fn u32_to_f64(x: u32) -> f64 { 82 | x as f64 83 | } 84 | 85 | #[test] 86 | fn test_u32_to_f64_happy() { 87 | let x = 3u32; 88 | let y = 3.0f64; 89 | assert_eq!(y, u32_to_f64(x)); 90 | } 91 | 92 | // String --------------------------------------------------------------- 93 | 94 | #[allow(dead_code)] 95 | fn string_to_i32(x: String) -> i32 { 96 | x.parse().unwrap() 97 | } 98 | 99 | #[test] 100 | fn test_string_to_i32_happy() { 101 | let x = "3".to_string(); 102 | let y = 3i32; 103 | assert_eq!(y, string_to_i32(x)); 104 | } 105 | 106 | #[allow(dead_code)] 107 | fn string_to_u32(x: String) -> u32 { 108 | x.parse().unwrap() 109 | } 110 | 111 | #[test] 112 | fn test_string_to_u32_happy() { 113 | let x = "3".to_string(); 114 | let y = 3u32; 115 | assert_eq!(y, string_to_u32(x)); 116 | } 117 | 118 | #[allow(dead_code)] 119 | fn string_to_f64(x: String) -> f64 { 120 | x.parse().unwrap() 121 | } 122 | 123 | #[test] 124 | fn test_string_to_f64_happy() { 125 | let x = "3.14".to_string(); 126 | let y = 3.14f64; 127 | assert_eq!(y, string_to_f64(x)); 128 | } 129 | 130 | // f64 --------------------------------------------------------------- 131 | 132 | #[allow(dead_code)] 133 | fn f64_to_i32(x: f64) -> i32 { 134 | x as i32 135 | } 136 | 137 | #[test] 138 | fn test_f64_to_i32_happy() { 139 | let x = 3.14f64; 140 | let y = 3i32; 141 | assert_eq!(y, f64_to_i32(x)); 142 | } 143 | 144 | #[allow(dead_code)] 145 | fn f64_to_u32(x: f64) -> u32 { 146 | x as u32 147 | } 148 | 149 | #[test] 150 | fn test_f64_to_u32_happy() { 151 | let x = 3.14f64; 152 | let y = 3u32; 153 | assert_eq!(y, f64_to_u32(x)); 154 | } 155 | 156 | #[allow(dead_code)] 157 | fn f64_to_string(x: f64) -> String { 158 | x.to_string() 159 | } 160 | 161 | #[test] 162 | fn test_f64_to_string_happy() { 163 | let x = 3.14f64; 164 | let y = "3.14"; 165 | assert_eq!(y, f64_to_string(x)); 166 | } 167 | 168 | // Option -------------------------------------------------------- 169 | 170 | #[test] 171 | fn test_option_handling_with_default_some() { 172 | let x = Some(1).unwrap_or(0); 173 | assert_eq!(x, 1); 174 | } 175 | 176 | #[test] 177 | fn test_parse_option_handling_with_default_none() { 178 | let x = None.unwrap_or(0); 179 | assert_eq!(x, 0); 180 | } 181 | 182 | #[test] 183 | fn test_parse_option_handling_expect_some() { 184 | let x = Some(1).expect("Parsing int from string failed"); 185 | assert_eq!(x, 1); 186 | } 187 | 188 | #[test] 189 | #[should_panic(expected = "Parsing int from string failed")] 190 | #[allow(unused_variables)] 191 | fn test_parse_option_handling_expect_none() { 192 | let x: i32 = None.expect("Parsing int from string failed"); 193 | } 194 | 195 | #[test] 196 | fn test_parse_option_handling_unwrap_some() { 197 | let x = Some(1).unwrap(); 198 | assert_eq!(x, 1); 199 | } 200 | 201 | #[test] 202 | #[should_panic(expected = "called `Option::unwrap()` on a `None` value")] 203 | #[allow(unused_variables)] 204 | fn test_parse_option_handling_unwrap_none() { 205 | let x: i32 = None.unwrap(); 206 | } 207 | 208 | // Vectors --------------------------------------------------------- 209 | 210 | #[test] 211 | fn test_vec_to_slice_happy() { 212 | let x = vec!(1u8, 2u8, 3u8); 213 | static Y: &'static [u8] = &[1, 2, 3]; 214 | assert_eq!(Y, &x[..]); 215 | } 216 | 217 | #[test] 218 | fn test_vec_to_boxedslice_happy() { 219 | let x = vec!(1u8, 2u8, 3u8); 220 | let y: Box<[u8]> = Box::new([1u8, 2, 3]); 221 | assert_eq!(y, x.into_boxed_slice()); 222 | } 223 | 224 | #[test] 225 | fn test_slice_to_vec_happy() { 226 | let x = &[1u8, 2, 3]; 227 | let y = vec!(1u8, 2u8, 3u8); 228 | assert_eq!(y, x.to_vec()); 229 | } 230 | 231 | #[test] 232 | fn test_slice_to_boxedslice_happy() { 233 | let x = &[1u8, 2, 3]; 234 | let y = Box::new([1u8, 2, 3]); 235 | assert_eq!(y, Box::new(*x)); 236 | } 237 | 238 | #[test] 239 | fn test_boxedslice_to_vec_happy() { 240 | let x: Box<[u8]> = Box::new([1u8, 2, 3]); 241 | let y = vec!(1u8, 2u8, 3u8); 242 | assert_eq!(y, x.into_vec()); 243 | } 244 | 245 | #[test] 246 | fn test_boxedslice_to_slice_happy() { 247 | let x: Box<[u8]> = Box::new([1u8, 2, 3]); 248 | let y = [1u8, 2, 3]; 249 | assert_eq!(y, &*x); 250 | } 251 | -------------------------------------------------------------------------------- /styles.css: -------------------------------------------------------------------------------- 1 | body { 2 | font-family: Garamond, Georgia, Times, "Times New Roman", serif; 3 | padding: 75px 100px; 4 | font-size: 16px; 5 | } 6 | h1 { 7 | font-size: 200%; 8 | font-weight: bold; 9 | } 10 | h2 { 11 | font-size: 150%; 12 | font-weight: bold; 13 | } 14 | section { 15 | margin: 40px 0; 16 | width: 700px; 17 | position: relative; 18 | } 19 | p { 20 | margin: 20px 0; 21 | } 22 | table { 23 | border-collapse: collapse; 24 | } 25 | table, th, td { 26 | border: 1px solid #999; 27 | } 28 | th, td { 29 | padding: 10px; 30 | width: 180px; 31 | } 32 | th { 33 | font-style: bold; 34 | } 35 | code, .autocode td { 36 | font-family: Courier, "Courier New", monospace; 37 | } 38 | td.na { 39 | font-family: Garamond, Georgia, Times, "Times New Roman", serif; 40 | color: #888; 41 | background: #efefef; 42 | } 43 | th.nowrap { 44 | white-space: nowrap; 45 | } 46 | ul { 47 | margin-left: 20px; 48 | } 49 | --------------------------------------------------------------------------------