|
147 | 147 | }, |
148 | 148 | { |
149 | 149 | "id": 24, |
150 | | - "length": 56, |
| 150 | + "length": 54, |
151 | 151 | "source": "Create a Tree data structure - programming-idioms.org", |
152 | | - "text": "struct Node<T> {\n value: T,\n children: Vec<Node<T>>,\n}" |
| 152 | + "text": "struct Node<T> {\n\tvalue: T,\n\tchildren: Vec<Node<T>>,\n}" |
153 | 153 | }, |
154 | 154 | { |
155 | 155 | "id": 25, |
|
201 | 201 | }, |
202 | 202 | { |
203 | 203 | "id": 33, |
204 | | - "length": 66, |
| 204 | + "length": 64, |
205 | 205 | "source": "Convert string to integer - programming-idioms.org", |
206 | | - "text": "let i = match s.parse::<i32>() {\n Ok(i) => i,\n Err(_e) => -1,\n};" |
| 206 | + "text": "let i = match s.parse::<i32>() {\n\tOk(i) => i,\n\tErr(_e) => -1,\n};" |
207 | 207 | }, |
208 | 208 | { |
209 | 209 | "id": 34, |
|
213 | 213 | }, |
214 | 214 | { |
215 | 215 | "id": 36, |
216 | | - "length": 211, |
| 216 | + "length": 209, |
217 | 217 | "source": "Send a value to another thread - programming-idioms.org", |
218 | | - "text": "use std::thread;\nuse std::sync::mpsc::channel;\nlet (send, recv) = channel();\nthread::spawn(move || {\n\tloop {\n\t\tlet msg = recv.recv().unwrap();\n\t\tprintln!(\"Hello, {:?}\", msg);\n\t} \n});\nsend.send(\"Alan\").unwrap();" |
| 218 | + "text": "use std::thread;\nuse std::sync::mpsc::channel;\nlet (send, recv) = channel();\nthread::spawn(move || {\n\tloop {\n\t\tlet msg = recv.recv().unwrap();\n\t\tprintln!(\"Hello, {:?}\", msg);\n\t}\n});\nsend.send(\"Alan\").unwrap();" |
219 | 219 | }, |
220 | 220 | { |
221 | 221 | "id": 37, |
|
279 | 279 | }, |
280 | 280 | { |
281 | 281 | "id": 47, |
282 | | - "length": 145, |
| 282 | + "length": 143, |
283 | 283 | "source": "Integer exponentiation by squaring - programming-idioms.org", |
284 | | - "text": "fn exp(x: u64, n: u64) -> u64 {\n\tmatch n {\n\t\t0 => 1,\n\t\t1 => x,\n\t\ti if i % 2 == 0 => exp(x * x, n / 2),\n\t\t_ => x * exp(x * x, (n - 1) / 2),\n\t}\t \n}" |
| 284 | + "text": "fn exp(x: u64, n: u64) -> u64 {\n\tmatch n {\n\t\t0 => 1,\n\t\t1 => x,\n\t\ti if i % 2 == 0 => exp(x * x, n / 2),\n\t\t_ => x * exp(x * x, (n - 1) / 2),\n\t}\n}" |
285 | 285 | }, |
286 | 286 | { |
287 | 287 | "id": 48, |
|
297 | 297 | }, |
298 | 298 | { |
299 | 299 | "id": 50, |
300 | | - "length": 145, |
| 300 | + "length": 149, |
301 | 301 | "source": "First-class function : compose - programming-idioms.org", |
302 | | - "text": "fn compose<'a, A, B, C, G, F>(f: F, g: G) -> Box<Fn(A) -> C + 'a>\n\t\twhere F: 'a + Fn(A) -> B, G: 'a + Fn(B) -> C\n{\n\t\tBox::new(move |x| g(f(x)))\n}" |
| 302 | + "text": "fn compose<'a, A, B, C, G, F>(f: F, g: G) -> Box<dyn Fn(A) -> C + 'a>\n\t\twhere F: 'a + Fn(A) -> B, G: 'a + Fn(B) -> C\n{\n\t\tBox::new(move |x| g(f(x)))\n}" |
303 | 303 | }, |
304 | 304 | { |
305 | 305 | "id": 51, |
|
309 | 309 | }, |
310 | 310 | { |
311 | 311 | "id": 52, |
312 | | - "length": 145, |
| 312 | + "length": 149, |
313 | 313 | "source": "First-class function : generic composition - programming-idioms.org", |
314 | | - "text": "fn compose<'a, A, B, C, G, F>(f: F, g: G) -> Box<Fn(A) -> C + 'a>\n\t\twhere F: 'a + Fn(A) -> B, G: 'a + Fn(B) -> C\n{\n\t\tBox::new(move |x| g(f(x)))\n}" |
| 314 | + "text": "fn compose<'a, A, B, C, G, F>(f: F, g: G) -> Box<dyn Fn(A) -> C + 'a>\n\t\twhere F: 'a + Fn(A) -> B, G: 'a + Fn(B) -> C\n{\n\t\tBox::new(move |x| g(f(x)))\n}" |
315 | 315 | }, |
316 | 316 | { |
317 | 317 | "id": 53, |
|
357 | 357 | }, |
358 | 358 | { |
359 | 359 | "id": 60, |
360 | | - "length": 103, |
| 360 | + "length": 104, |
361 | 361 | "source": "Continue outer loop - programming-idioms.org", |
362 | | - "text": "outer: for va in &a {\n\tfor vb in &b {\n\t\tif va == vb {\n\t\t\tcontinue 'outer;\n\t\t}\n\t}\n\tprintln!(\"{}\", va);\n}" |
| 362 | + "text": "'outer: for va in &a {\n\tfor vb in &b {\n\t\tif va == vb {\n\t\t\tcontinue 'outer;\n\t\t}\n\t}\n\tprintln!(\"{}\", va);\n}" |
363 | 363 | }, |
364 | 364 | { |
365 | 365 | "id": 61, |
366 | | - "length": 108, |
| 366 | + "length": 109, |
367 | 367 | "source": "Break outer loop - programming-idioms.org", |
368 | | - "text": "outer: for v in m {\n\t'inner: for i in v {\n\t\tif i < 0 {\n\t\t\tprintln!(\"Found {}\", i);\n\t\t\tbreak 'outer;\n\t\t}\n\t}\n}" |
| 368 | + "text": "'outer: for v in m {\n\t'inner: for i in v {\n\t\tif i < 0 {\n\t\t\tprintln!(\"Found {}\", i);\n\t\t\tbreak 'outer;\n\t\t}\n\t}\n}" |
369 | 369 | }, |
370 | 370 | { |
371 | 371 | "id": 62, |
|
537 | 537 | }, |
538 | 538 | { |
539 | 539 | "id": 90, |
540 | | - "length": 267, |
| 540 | + "length": 266, |
541 | 541 | "source": "Binomial coefficient \"n choose k\" - programming-idioms.org", |
542 | | - "text": "extern crate num;\nuse num::bigint::BigInt;\nuse num::bigint::ToBigInt;\nuse num::traits::One;\nfn binom(n: u64, k: u64) -> BigInt {\n\tlet mut res = BigInt::one();\n\tfor i in 0..k {\n\t\tres = (res * (n - i).to_bigint().unwrap()) /\n\t\t\t (i + 1).to_bigint().unwrap();\n\t}\n\tres\n}" |
| 542 | + "text": "extern crate num;\nuse num::bigint::BigInt;\nuse num::bigint::ToBigInt;\nuse num::traits::One;\nfn binom(n: u64, k: u64) -> BigInt {\n\tlet mut res = BigInt::one();\n\tfor i in 0..k {\n\t\tres = (res * (n - i).to_bigint().unwrap()) /\n\t\t\t\t(i + 1).to_bigint().unwrap();\n\t}\n\tres\n}" |
543 | 543 | }, |
544 | 544 | { |
545 | 545 | "id": 91, |
|
963 | 963 | }, |
964 | 964 | { |
965 | 965 | "id": 161, |
966 | | - "length": 87, |
| 966 | + "length": 88, |
967 | 967 | "source": "Measure duration of procedure execution - programming-idioms.org", |
968 | | - "text": "use std::time::Instant;\nlet start = Instant:now();\nf();\nlet duration = start.elapsed();" |
| 968 | + "text": "use std::time::Instant;\nlet start = Instant::now();\nf();\nlet duration = start.elapsed();" |
969 | 969 | }, |
970 | 970 | { |
971 | 971 | "id": 162, |
|
1167 | 1167 | }, |
1168 | 1168 | { |
1169 | 1169 | "id": 195, |
1170 | | - "length": 204, |
| 1170 | + "length": 205, |
1171 | 1171 | "source": "Execute procedures depending on options - programming-idioms.org", |
1172 | | - "text": "if let Some(arg) = ::std::env::args().nth(1) {\n\tif &arg == \"f\" {\n\t\tfox();\n\t} else if &arg = \"b\" {\n\t\tbat();\n\t} else {\n\t\teprintln!(\"invalid argument: {}\", arg),\n\t}\n} else {\n\teprintln!(\"missing argument\");\n}" |
| 1172 | + "text": "if let Some(arg) = ::std::env::args().nth(1) {\n\tif &arg == \"f\" {\n\t\tfox();\n\t} else if &arg == \"b\" {\n\t\tbat();\n\t} else {\n\t\teprintln!(\"invalid argument: {}\", arg);\n\t}\n} else {\n\teprintln!(\"missing argument\");\n}" |
1173 | 1173 | }, |
1174 | 1174 | { |
1175 | 1175 | "id": 196, |
|
1257 | 1257 | }, |
1258 | 1258 | { |
1259 | 1259 | "id": 210, |
1260 | | - "length": 45, |
| 1260 | + "length": 46, |
1261 | 1261 | "source": "Insert entry in map - programming-idioms.org", |
1262 | | - "text": "use std::collection::HashMap;\nm.insert(k, v);" |
| 1262 | + "text": "use std::collections::HashMap;\nm.insert(k, v);" |
1263 | 1263 | }, |
1264 | 1264 | { |
1265 | 1265 | "id": 211, |
|
1287 | 1287 | }, |
1288 | 1288 | { |
1289 | 1289 | "id": 215, |
1290 | | - "length": 80, |
| 1290 | + "length": 81, |
1291 | 1291 | "source": "Hex string to byte array - programming-idioms.org", |
1292 | | - "text": "use hex::FromHex\nlet a: Vec<u8> = Vec::from_hex(s).expect(\"Invalid Hex String\");" |
| 1292 | + "text": "use hex::FromHex;\nlet a: Vec<u8> = Vec::from_hex(s).expect(\"Invalid Hex String\");" |
1293 | 1293 | }, |
1294 | 1294 | { |
1295 | 1295 | "id": 216, |
|
1311 | 1311 | }, |
1312 | 1312 | { |
1313 | 1313 | "id": 219, |
1314 | | - "length": 61, |
| 1314 | + "length": 62, |
1315 | 1315 | "source": "List files in directory - programming-idioms.org", |
1316 | | - "text": "let x = std::fs::read_dir(d)?.collect::<Result<Vec<_>, _>()?;" |
| 1316 | + "text": "let x = std::fs::read_dir(d)?.collect::<Result<Vec<_>, _>>()?;" |
1317 | 1317 | }, |
1318 | 1318 | { |
1319 | 1319 | "id": 220, |
|
1341 | 1341 | }, |
1342 | 1342 | { |
1343 | 1343 | "id": 224, |
1344 | | - "length": 48, |
| 1344 | + "length": 47, |
1345 | 1345 | "source": "Exit program cleanly - programming-idioms.org", |
1346 | | - "text": "use std::process::exit;\nfn main() {\n exit(0);\n}" |
| 1346 | + "text": "use std::process::exit;\nfn main() {\n\texit(0);\n}" |
1347 | 1347 | }, |
1348 | 1348 | { |
1349 | 1349 | "id": 225, |
|
1461 | 1461 | }, |
1462 | 1462 | { |
1463 | 1463 | "id": 244, |
1464 | | - "length": 67, |
| 1464 | + "length": 65, |
1465 | 1465 | "source": "Pad string on the right - programming-idioms.org", |
1466 | | - "text": "use std::iter();\ns += &iter::repeat(c).take(m).collect::<String>();" |
| 1466 | + "text": "use std::iter;\ns += &iter::repeat(c).take(m).collect::<String>();" |
1467 | 1467 | }, |
1468 | 1468 | { |
1469 | 1469 | "id": 245, |
1470 | | - "length": 451, |
| 1470 | + "length": 453, |
1471 | 1471 | "source": "Pad string on the left - programming-idioms.org", |
1472 | | - "text": "use unicode_width::{UnicodeWidthChar, UnicodeWidthStr};\nif let Some(columns_short) = m.checked_sub(s.width()) {\n\tlet padding_width = c\n\t\t.width()\n\t\t.filter(|n| *n > 0)\n\t\t.expect(\"padding character should be visible\");\n\t// Saturate the columns_short\n\tlet padding_needed = columns_short + padding_width - 1 / padding_width;\n\tlet mut t = String::with_capacity(s.len() + padding_needed);\n\tt.extend((0..padding_needed).map(|_| c)\n\tt.push_str(&s);\n\ts = t;\n}" |
| 1472 | + "text": "use unicode_width::{UnicodeWidthChar, UnicodeWidthStr};\nif let Some(columns_short) = m.checked_sub(s.width()) {\n\tlet padding_width = c\n\t\t.width()\n\t\t.filter(|n| *n > 0)\n\t\t.expect(\"padding character should be visible\");\n\t// Saturate the columns_short\n\tlet padding_needed = columns_short + padding_width - 1 / padding_width;\n\tlet mut t = String::with_capacity(s.len() + padding_needed);\n\tt.extend((0..padding_needed).map(|_| c));\n\tt.push_str(&s);\n\ts = t;\n}" |
1473 | 1473 | }, |
1474 | 1474 | { |
1475 | 1475 | "id": 246, |
|
1491 | 1491 | }, |
1492 | 1492 | { |
1493 | 1493 | "id": 249, |
1494 | | - "length": 188, |
| 1494 | + "length": 190, |
1495 | 1495 | "source": "List intersection - programming-idioms.org", |
1496 | | - "text": "use std::collections::HashSet;\nlet unique_a = a.iter().collect::<HashSet<_>>();\nlet unique_b = b.iter().collect::<HashSet<_>>();\nlet c = unique_a.intersection(&unique_b).collect<Vec<_>>();" |
| 1496 | + "text": "use std::collections::HashSet;\nlet unique_a = a.iter().collect::<HashSet<_>>();\nlet unique_b = b.iter().collect::<HashSet<_>>();\nlet c = unique_a.intersection(&unique_b).collect::<Vec<_>>();" |
1497 | 1497 | }, |
1498 | 1498 | { |
1499 | 1499 | "id": 250, |
|
1521 | 1521 | }, |
1522 | 1522 | { |
1523 | 1523 | "id": 254, |
1524 | | - "length": 118, |
| 1524 | + "length": 114, |
1525 | 1525 | "source": "Find first index of an element in list - programming-idioms.org", |
1526 | | - "text": "let opt_i = items.iter().position(|y| *y == x);\nlet i = match opt_i {\n Some(index) => index as i32,\n None => -1\n};" |
| 1526 | + "text": "let opt_i = items.iter().position(|y| *y == x);\nlet i = match opt_i {\n\tSome(index) => index as i32,\n\tNone => -1\n};" |
1527 | 1527 | }, |
1528 | 1528 | { |
1529 | 1529 | "id": 255, |
|
1557 | 1557 | }, |
1558 | 1558 | { |
1559 | 1559 | "id": 260, |
1560 | | - "length": 112, |
| 1560 | + "length": 111, |
1561 | 1561 | "source": "Declare and use an optional argument - programming-idioms.org", |
1562 | | - "text": "fn f(x: Option<()>) {\n\tmatch x {\n\t\tSome(x) => println!(\"Present {}\", x),\n\t\tNone => println!(\"Not present\"),\n\t}\n}" |
| 1562 | + "text": "fn f(x: Option<T>) {\n\tmatch x {\n\t\tSome(x) => println!(\"Present {}\", x),\n\t\tNone => println!(\"Not present\"),\n\t}\n}" |
1563 | 1563 | }, |
1564 | 1564 | { |
1565 | 1565 | "id": 261, |
|
1619 | 1619 | "id": 270, |
1620 | 1620 | "length": 156, |
1621 | 1621 | "source": "Sort 2 lists together - programming-idioms.org", |
1622 | | - "text": "let mut tmp: Vec<_> = a.iter().zip(b).collect();\ntmp.as_mut_slice().sort_by_key(|(&x, _y)| x);\nlet (aa, bb): (Vec<i32>, Vec<i32>) = tmp.into_iter().unzip();" |
| 1622 | + "text": "let mut tmp: Vec<_> = a.iter().zip(b).collect();\ntmp.as_mut_slice().sort_by_key(|&(x, _y)| x);\nlet (aa, bb): (Vec<i32>, Vec<i32>) = tmp.into_iter().unzip();" |
1623 | 1623 | }, |
1624 | 1624 | { |
1625 | 1625 | "id": 271, |
|
0 commit comments