Skip to content

Commit 231cd2e

Browse files
authored
fix(quotes): code_rust compilation and whitespace (@nafets-st) (#6755)
### Description * Fixes various compile errors in the Rust quotes. * Removes trailing whitespace. * Converts leading whitespace to tabs. Note: This does not fix out of date issues (such as the rand API), or the likely unnecessary uses of `extern crate` (since rust 2018).
1 parent 62a5145 commit 231cd2e

File tree

1 file changed

+41
-41
lines changed

1 file changed

+41
-41
lines changed

frontend/static/quotes/code_rust.json

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,9 @@
147147
},
148148
{
149149
"id": 24,
150-
"length": 56,
150+
"length": 54,
151151
"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}"
153153
},
154154
{
155155
"id": 25,
@@ -201,9 +201,9 @@
201201
},
202202
{
203203
"id": 33,
204-
"length": 66,
204+
"length": 64,
205205
"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};"
207207
},
208208
{
209209
"id": 34,
@@ -213,9 +213,9 @@
213213
},
214214
{
215215
"id": 36,
216-
"length": 211,
216+
"length": 209,
217217
"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();"
219219
},
220220
{
221221
"id": 37,
@@ -279,9 +279,9 @@
279279
},
280280
{
281281
"id": 47,
282-
"length": 145,
282+
"length": 143,
283283
"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}"
285285
},
286286
{
287287
"id": 48,
@@ -297,9 +297,9 @@
297297
},
298298
{
299299
"id": 50,
300-
"length": 145,
300+
"length": 149,
301301
"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}"
303303
},
304304
{
305305
"id": 51,
@@ -309,9 +309,9 @@
309309
},
310310
{
311311
"id": 52,
312-
"length": 145,
312+
"length": 149,
313313
"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}"
315315
},
316316
{
317317
"id": 53,
@@ -357,15 +357,15 @@
357357
},
358358
{
359359
"id": 60,
360-
"length": 103,
360+
"length": 104,
361361
"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}"
363363
},
364364
{
365365
"id": 61,
366-
"length": 108,
366+
"length": 109,
367367
"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}"
369369
},
370370
{
371371
"id": 62,
@@ -537,9 +537,9 @@
537537
},
538538
{
539539
"id": 90,
540-
"length": 267,
540+
"length": 266,
541541
"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}"
543543
},
544544
{
545545
"id": 91,
@@ -963,9 +963,9 @@
963963
},
964964
{
965965
"id": 161,
966-
"length": 87,
966+
"length": 88,
967967
"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();"
969969
},
970970
{
971971
"id": 162,
@@ -1167,9 +1167,9 @@
11671167
},
11681168
{
11691169
"id": 195,
1170-
"length": 204,
1170+
"length": 205,
11711171
"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}"
11731173
},
11741174
{
11751175
"id": 196,
@@ -1257,9 +1257,9 @@
12571257
},
12581258
{
12591259
"id": 210,
1260-
"length": 45,
1260+
"length": 46,
12611261
"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);"
12631263
},
12641264
{
12651265
"id": 211,
@@ -1287,9 +1287,9 @@
12871287
},
12881288
{
12891289
"id": 215,
1290-
"length": 80,
1290+
"length": 81,
12911291
"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\");"
12931293
},
12941294
{
12951295
"id": 216,
@@ -1311,9 +1311,9 @@
13111311
},
13121312
{
13131313
"id": 219,
1314-
"length": 61,
1314+
"length": 62,
13151315
"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<_>, _>>()?;"
13171317
},
13181318
{
13191319
"id": 220,
@@ -1341,9 +1341,9 @@
13411341
},
13421342
{
13431343
"id": 224,
1344-
"length": 48,
1344+
"length": 47,
13451345
"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}"
13471347
},
13481348
{
13491349
"id": 225,
@@ -1461,15 +1461,15 @@
14611461
},
14621462
{
14631463
"id": 244,
1464-
"length": 67,
1464+
"length": 65,
14651465
"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>();"
14671467
},
14681468
{
14691469
"id": 245,
1470-
"length": 451,
1470+
"length": 453,
14711471
"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}"
14731473
},
14741474
{
14751475
"id": 246,
@@ -1491,9 +1491,9 @@
14911491
},
14921492
{
14931493
"id": 249,
1494-
"length": 188,
1494+
"length": 190,
14951495
"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<_>>();"
14971497
},
14981498
{
14991499
"id": 250,
@@ -1521,9 +1521,9 @@
15211521
},
15221522
{
15231523
"id": 254,
1524-
"length": 118,
1524+
"length": 114,
15251525
"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};"
15271527
},
15281528
{
15291529
"id": 255,
@@ -1557,9 +1557,9 @@
15571557
},
15581558
{
15591559
"id": 260,
1560-
"length": 112,
1560+
"length": 111,
15611561
"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}"
15631563
},
15641564
{
15651565
"id": 261,
@@ -1619,7 +1619,7 @@
16191619
"id": 270,
16201620
"length": 156,
16211621
"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();"
16231623
},
16241624
{
16251625
"id": 271,

0 commit comments

Comments
 (0)