]> git.lizzy.rs Git - rust.git/blob - doc/po/tutorial-container.md.pot
a93d5801dccf8b26a68426a672516dc5830dbfe1
[rust.git] / doc / po / tutorial-container.md.pot
1 # SOME DESCRIPTIVE TITLE
2 # Copyright (C) YEAR The Rust Project Developers
3 # This file is distributed under the same license as the Rust package.
4 # FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
5 #
6 #, fuzzy
7 msgid ""
8 msgstr ""
9 "Project-Id-Version: Rust 0.8-pre\n"
10 "POT-Creation-Date: 2013-08-05 19:40+0900\n"
11 "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
12 "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
13 "Language-Team: LANGUAGE <LL@li.org>\n"
14 "Language: \n"
15 "MIME-Version: 1.0\n"
16 "Content-Type: text/plain; charset=UTF-8\n"
17 "Content-Transfer-Encoding: 8bit\n"
18
19 #. type: Plain text
20 #: doc/tutorial-container.md:2
21 msgid "% Containers and iterators"
22 msgstr ""
23
24 #. type: Plain text
25 #: doc/tutorial-container.md:4
26 msgid "# Containers"
27 msgstr ""
28
29 #. type: Plain text
30 #: doc/tutorial-container.md:6
31 msgid "The container traits are defined in the `std::container` module."
32 msgstr ""
33
34 #. type: Plain text
35 #: doc/tutorial-container.md:8
36 msgid "## Unique and managed vectors"
37 msgstr ""
38
39 #. type: Plain text
40 #: doc/tutorial-container.md:12
41 msgid ""
42 "Vectors have `O(1)` indexing and removal from the end, along with `O(1)` "
43 "amortized insertion. Vectors are the most common container in Rust, and are "
44 "flexible enough to fit many use cases."
45 msgstr ""
46
47 #. type: Plain text
48 #: doc/tutorial-container.md:16
49 msgid ""
50 "Vectors can also be sorted and used as efficient lookup tables with the "
51 "`std::vec::bsearch` function, if all the elements are inserted at one time "
52 "and deletions are unnecessary."
53 msgstr ""
54
55 #. type: Plain text
56 #: doc/tutorial-container.md:18
57 msgid "## Maps and sets"
58 msgstr ""
59
60 #. type: Plain text
61 #: doc/tutorial-container.md:22
62 msgid ""
63 "Maps are collections of unique keys with corresponding values, and sets are "
64 "just unique keys without a corresponding value. The `Map` and `Set` traits "
65 "in `std::container` define the basic interface."
66 msgstr ""
67
68 #. type: Plain text
69 #: doc/tutorial-container.md:24
70 msgid "The standard library provides three owned map/set types:"
71 msgstr ""
72
73 #. type: Bullet: '* '
74 #: doc/tutorial-container.md:30
75 msgid ""
76 "`std::hashmap::HashMap` and `std::hashmap::HashSet`, requiring the keys to "
77 "implement `Eq` and `Hash`"
78 msgstr ""
79
80 #. type: Bullet: '* '
81 #: doc/tutorial-container.md:30
82 msgid ""
83 "`std::trie::TrieMap` and `std::trie::TrieSet`, requiring the keys to be "
84 "`uint`"
85 msgstr ""
86
87 #. type: Bullet: '* '
88 #: doc/tutorial-container.md:30
89 msgid ""
90 "`extra::treemap::TreeMap` and `extra::treemap::TreeSet`, requiring the keys "
91 "to implement `TotalOrd`"
92 msgstr ""
93
94 #. type: Plain text
95 #: doc/tutorial-container.md:34
96 msgid ""
97 "These maps do not use managed pointers so they can be sent between tasks as "
98 "long as the key and value types are sendable. Neither the key or value type "
99 "has to be copyable."
100 msgstr ""
101
102 #. type: Plain text
103 #: doc/tutorial-container.md:37
104 msgid ""
105 "The `TrieMap` and `TreeMap` maps are ordered, while `HashMap` uses an "
106 "arbitrary order."
107 msgstr ""
108
109 #. type: Plain text
110 #: doc/tutorial-container.md:42
111 msgid ""
112 "Each `HashMap` instance has a random 128-bit key to use with a keyed hash, "
113 "making the order of a set of keys in a given hash table randomized. Rust "
114 "provides a [SipHash](https://131002.net/siphash/) implementation for any "
115 "type implementing the `IterBytes` trait."
116 msgstr ""
117
118 #. type: Plain text
119 #: doc/tutorial-container.md:44
120 msgid "## Double-ended queues"
121 msgstr ""
122
123 #. type: Plain text
124 #: doc/tutorial-container.md:49
125 msgid ""
126 "The `extra::deque` module implements a double-ended queue with `O(1)` "
127 "amortized inserts and removals from both ends of the container. It also has "
128 "`O(1)` indexing like a vector. The contained elements are not required to be "
129 "copyable, and the queue will be sendable if the contained type is sendable."
130 msgstr ""
131
132 #. type: Plain text
133 #: doc/tutorial-container.md:51
134 msgid "## Priority queues"
135 msgstr ""
136
137 #. type: Plain text
138 #: doc/tutorial-container.md:55
139 msgid ""
140 "The `extra::priority_queue` module implements a queue ordered by a key.  The "
141 "contained elements are not required to be copyable, and the queue will be "
142 "sendable if the contained type is sendable."
143 msgstr ""
144
145 #. type: Plain text
146 #: doc/tutorial-container.md:61
147 msgid ""
148 "Insertions have `O(log n)` time complexity and checking or popping the "
149 "largest element is `O(1)`. Converting a vector to a priority queue can be "
150 "done in-place, and has `O(n)` complexity. A priority queue can also be "
151 "converted to a sorted vector in-place, allowing it to be used for an `O(n "
152 "log n)` in-place heapsort."
153 msgstr ""
154
155 #. type: Plain text
156 #: doc/tutorial-container.md:63
157 msgid "# Iterators"
158 msgstr ""
159
160 #. type: Plain text
161 #: doc/tutorial-container.md:65
162 msgid "## Iteration protocol"
163 msgstr ""
164
165 #. type: Plain text
166 #: doc/tutorial-container.md:69
167 msgid ""
168 "The iteration protocol is defined by the `Iterator` trait in the `std::"
169 "iterator` module. The minimal implementation of the trait is a `next` "
170 "method, yielding the next element from an iterator object:"
171 msgstr ""
172
173 #. type: Plain text
174 #: doc/tutorial-container.md:73
175 msgid "~~~ /// An infinite stream of zeroes struct ZeroStream;"
176 msgstr ""
177
178 #. type: Plain text
179 #: doc/tutorial-container.md:80
180 #, no-wrap
181 msgid ""
182 "impl Iterator<int> for ZeroStream {\n"
183 "    fn next(&mut self) -> Option<int> {\n"
184 "        Some(0)\n"
185 "    }\n"
186 "}\n"
187 "~~~~\n"
188 msgstr ""
189
190 #. type: Plain text
191 #: doc/tutorial-container.md:83
192 msgid ""
193 "Reaching the end of the iterator is signalled by returning `None` instead of "
194 "`Some(item)`:"
195 msgstr ""
196
197 #. type: Plain text
198 #: doc/tutorial-container.md:89 doc/tutorial-container.md:262
199 #, no-wrap
200 msgid ""
201 "~~~\n"
202 "/// A stream of N zeroes\n"
203 "struct ZeroStream {\n"
204 "    priv remaining: uint\n"
205 "}\n"
206 msgstr ""
207
208 #. type: Plain text
209 #: doc/tutorial-container.md:95
210 #, no-wrap
211 msgid ""
212 "impl ZeroStream {\n"
213 "    fn new(n: uint) -> ZeroStream {\n"
214 "        ZeroStream { remaining: n }\n"
215 "    }\n"
216 "}\n"
217 msgstr ""
218
219 #. type: Plain text
220 #: doc/tutorial-container.md:107 doc/tutorial-container.md:284
221 #, no-wrap
222 msgid ""
223 "impl Iterator<int> for ZeroStream {\n"
224 "    fn next(&mut self) -> Option<int> {\n"
225 "        if self.remaining == 0 {\n"
226 "            None\n"
227 "        } else {\n"
228 "            self.remaining -= 1;\n"
229 "            Some(0)\n"
230 "        }\n"
231 "    }\n"
232 "}\n"
233 "~~~\n"
234 msgstr ""
235
236 #. type: Plain text
237 #: doc/tutorial-container.md:109
238 msgid "## Container iterators"
239 msgstr ""
240
241 #. type: Plain text
242 #: doc/tutorial-container.md:112
243 msgid ""
244 "Containers implement iteration over the contained elements by returning an "
245 "iterator object. For example, vector slices several iterators available:"
246 msgstr ""
247
248 #. type: Bullet: '* '
249 #: doc/tutorial-container.md:116
250 msgid "`iter()` and `rev_iter()`, for immutable references to the elements"
251 msgstr ""
252
253 #. type: Bullet: '* '
254 #: doc/tutorial-container.md:116
255 msgid ""
256 "`mut_iter()` and `mut_rev_iter()`, for mutable references to the elements"
257 msgstr ""
258
259 #. type: Bullet: '* '
260 #: doc/tutorial-container.md:116
261 msgid ""
262 "`consume_iter()` and `consume_rev_iter`, to move the elements out by-value"
263 msgstr ""
264
265 #. type: Plain text
266 #: doc/tutorial-container.md:119
267 msgid ""
268 "A typical mutable container will implement at least `iter()`, `mut_iter()` "
269 "and `consume_iter()` along with the reverse variants if it maintains an "
270 "order."
271 msgstr ""
272
273 #. type: Plain text
274 #: doc/tutorial-container.md:121
275 msgid "### Freezing"
276 msgstr ""
277
278 #. type: Plain text
279 #: doc/tutorial-container.md:125
280 msgid ""
281 "Unlike most other languages with external iterators, Rust has no *iterator "
282 "invalidation*. As long an iterator is still in scope, the compiler will "
283 "prevent modification of the container through another handle."
284 msgstr ""
285
286 #. type: Plain text
287 #: doc/tutorial-container.md:130
288 #, no-wrap
289 msgid ""
290 "~~~\n"
291 "let mut xs = [1, 2, 3];\n"
292 "{\n"
293 "    let _it = xs.iter();\n"
294 msgstr ""
295
296 #. type: Plain text
297 #: doc/tutorial-container.md:136
298 #, no-wrap
299 msgid ""
300 "    // the vector is frozen for this scope, the compiler will statically\n"
301 "    // prevent modification\n"
302 "}\n"
303 "// the vector becomes unfrozen again at the end of the scope\n"
304 "~~~\n"
305 msgstr ""
306
307 #. type: Plain text
308 #: doc/tutorial-container.md:139
309 msgid ""
310 "These semantics are due to most container iterators being implemented with "
311 "`&` and `&mut`."
312 msgstr ""
313
314 #. type: Plain text
315 #: doc/tutorial-container.md:141
316 msgid "## Iterator adaptors"
317 msgstr ""
318
319 #. type: Plain text
320 #: doc/tutorial-container.md:145
321 msgid ""
322 "The `IteratorUtil` trait implements common algorithms as methods extending "
323 "every `Iterator` implementation. For example, the `fold` method will "
324 "accumulate the items yielded by an `Iterator` into a single value:"
325 msgstr ""
326
327 #. type: Plain text
328 #: doc/tutorial-container.md:151
329 msgid ""
330 "~~~ let xs = [1, 9, 2, 3, 14, 12]; let result = xs.iter().fold(0, |"
331 "accumulator, item| accumulator - *item); assert_eq!(result, -41); ~~~"
332 msgstr ""
333
334 #. type: Plain text
335 #: doc/tutorial-container.md:153
336 msgid ""
337 "Some adaptors return an adaptor object implementing the `Iterator` trait "
338 "itself:"
339 msgstr ""
340
341 #. type: Plain text
342 #: doc/tutorial-container.md:160
343 msgid ""
344 "~~~ let xs = [1, 9, 2, 3, 14, 12]; let ys = [5, 2, 1, 8]; let sum = xs."
345 "iter().chain_(ys.iter()).fold(0, |a, b| a + *b); assert_eq!(sum, 57); ~~~"
346 msgstr ""
347
348 #. type: Plain text
349 #: doc/tutorial-container.md:164
350 msgid ""
351 "Note that some adaptors like the `chain_` method above use a trailing "
352 "underscore to work around an issue with method resolve. The underscores will "
353 "be dropped when they become unnecessary."
354 msgstr ""
355
356 #. type: Plain text
357 #: doc/tutorial-container.md:166
358 msgid "## For loops"
359 msgstr ""
360
361 #. type: Plain text
362 #: doc/tutorial-container.md:168
363 msgid ""
364 "The `for` keyword can be used as sugar for iterating through any iterator:"
365 msgstr ""
366
367 #. type: Plain text
368 #: doc/tutorial-container.md:171
369 msgid "~~~ let xs = [2, 3, 5, 7, 11, 13, 17];"
370 msgstr ""
371
372 #. type: Plain text
373 #: doc/tutorial-container.md:176
374 #, no-wrap
375 msgid ""
376 "// print out all the elements in the vector\n"
377 "for x in xs.iter() {\n"
378 "    println(x.to_str())\n"
379 "}\n"
380 msgstr ""
381
382 #. type: Plain text
383 #: doc/tutorial-container.md:182
384 #, no-wrap
385 msgid ""
386 "// print out all but the first 3 elements in the vector\n"
387 "for x in xs.iter().skip(3) {\n"
388 "    println(x.to_str())\n"
389 "}\n"
390 "~~~\n"
391 msgstr ""
392
393 #. type: Plain text
394 #: doc/tutorial-container.md:185
395 msgid ""
396 "For loops are *often* used with a temporary iterator object, as above. They "
397 "can also advance the state of an iterator in a mutable location:"
398 msgstr ""
399
400 #. type: Plain text
401 #: doc/tutorial-container.md:189
402 msgid ""
403 "~~~ let xs = [1, 2, 3, 4, 5]; let ys = [\"foo\", \"bar\", \"baz\", \"foobar"
404 "\"];"
405 msgstr ""
406
407 #. type: Plain text
408 #: doc/tutorial-container.md:192
409 msgid ""
410 "// create an iterator yielding tuples of elements from both vectors let mut "
411 "it = xs.iter().zip(ys.iter());"
412 msgstr ""
413
414 #. type: Plain text
415 #: doc/tutorial-container.md:196
416 #, no-wrap
417 msgid ""
418 "// print out the pairs of elements up to (&3, &\"baz\")\n"
419 "for (x, y) in it {\n"
420 "    printfln!(\"%d %s\", *x, *y);\n"
421 msgstr ""
422
423 #. type: Plain text
424 #: doc/tutorial-container.md:201
425 #, no-wrap
426 msgid ""
427 "    if *x == 3 {\n"
428 "        break;\n"
429 "    }\n"
430 "}\n"
431 msgstr ""
432
433 #. type: Plain text
434 #: doc/tutorial-container.md:204
435 msgid ""
436 "// yield and print the last pair from the iterator printfln!(\"last: %?\", "
437 "it.next());"
438 msgstr ""
439
440 #. type: Plain text
441 #: doc/tutorial-container.md:208
442 msgid "// the iterator is now fully consumed assert!(it.next().is_none()); ~~~"
443 msgstr ""
444
445 #. type: Plain text
446 #: doc/tutorial-container.md:210
447 msgid "## Conversion"
448 msgstr ""
449
450 #. type: Plain text
451 #: doc/tutorial-container.md:212
452 msgid ""
453 "Iterators offer generic conversion to containers with the `collect` adaptor:"
454 msgstr ""
455
456 #. type: Plain text
457 #: doc/tutorial-container.md:218
458 msgid ""
459 "~~~ let xs = [0, 1, 1, 2, 3, 5, 8]; let ys = xs.rev_iter().skip(1)."
460 "transform(|&x| x * 2).collect::<~[int]>(); assert_eq!(ys, ~[10, 6, 4, 2, 2, "
461 "0]); ~~~"
462 msgstr ""
463
464 #. type: Plain text
465 #: doc/tutorial-container.md:221
466 msgid ""
467 "The method requires a type hint for the container type, if the surrounding "
468 "code does not provide sufficient information."
469 msgstr ""
470
471 #. type: Plain text
472 #: doc/tutorial-container.md:225
473 msgid ""
474 "Containers can provide conversion from iterators through `collect` by "
475 "implementing the `FromIterator` trait. For example, the implementation for "
476 "vectors is as follows:"
477 msgstr ""
478
479 #. type: Plain text
480 #: doc/tutorial-container.md:238
481 #, no-wrap
482 msgid ""
483 "~~~\n"
484 "impl<A> FromIterator<A> for ~[A] {\n"
485 "    pub fn from_iterator<T: Iterator<A>>(iterator: &mut T) -> ~[A] {\n"
486 "        let (lower, _) = iterator.size_hint();\n"
487 "        let mut xs = with_capacity(lower);\n"
488 "        for x in iterator {\n"
489 "            xs.push(x);\n"
490 "        }\n"
491 "        xs\n"
492 "    }\n"
493 "}\n"
494 "~~~\n"
495 msgstr ""
496
497 #. type: Plain text
498 #: doc/tutorial-container.md:240
499 msgid "### Size hints"
500 msgstr ""
501
502 #. type: Plain text
503 #: doc/tutorial-container.md:243
504 msgid ""
505 "The `Iterator` trait provides a `size_hint` default method, returning a "
506 "lower bound and optionally on upper bound on the length of the iterator:"
507 msgstr ""
508
509 #. type: Plain text
510 #: doc/tutorial-container.md:247
511 msgid "~~~ fn size_hint(&self) -> (uint, Option<uint>) { (0, None) } ~~~"
512 msgstr ""
513
514 #. type: Plain text
515 #: doc/tutorial-container.md:251
516 msgid ""
517 "The vector implementation of `FromIterator` from above uses the lower bound "
518 "to pre-allocate enough space to hold the minimum number of elements the "
519 "iterator will yield."
520 msgstr ""
521
522 #. type: Plain text
523 #: doc/tutorial-container.md:254
524 msgid ""
525 "The default implementation is always correct, but it should be overridden if "
526 "the iterator can provide better information."
527 msgstr ""
528
529 #. type: Plain text
530 #: doc/tutorial-container.md:256
531 msgid ""
532 "The `ZeroStream` from earlier can provide an exact lower and upper bound:"
533 msgstr ""
534
535 #. type: Plain text
536 #: doc/tutorial-container.md:267
537 #, no-wrap
538 msgid ""
539 "impl ZeroStream {\n"
540 "    fn new(n: uint) -> ZeroStream {\n"
541 "        ZeroStream { remaining: n }\n"
542 "    }\n"
543 msgstr ""
544
545 #. type: Plain text
546 #: doc/tutorial-container.md:272
547 #, no-wrap
548 msgid ""
549 "    fn size_hint(&self) -> (uint, Option<uint>) {\n"
550 "        (self.remaining, Some(self.remaining))\n"
551 "    }\n"
552 "}\n"
553 msgstr ""
554
555 #. type: Plain text
556 #: doc/tutorial-container.md:286
557 msgid "## Double-ended iterators"
558 msgstr ""
559
560 #. type: Plain text
561 #: doc/tutorial-container.md:290
562 msgid ""
563 "The `DoubleEndedIterator` trait represents an iterator able to yield "
564 "elements from either end of a range. It inherits from the `Iterator` trait "
565 "and extends it with the `next_back` function."
566 msgstr ""
567
568 #. type: Plain text
569 #: doc/tutorial-container.md:293
570 msgid ""
571 "A `DoubleEndedIterator` can be flipped with the `invert` adaptor, returning "
572 "another `DoubleEndedIterator` with `next` and `next_back` exchanged."
573 msgstr ""
574
575 #. type: Plain text
576 #: doc/tutorial-container.md:300
577 msgid ""
578 "~~~ let xs = [1, 2, 3, 4, 5, 6]; let mut it = xs.iter(); printfln!(\"%?\", "
579 "it.next()); // prints `Some(&1)` printfln!(\"%?\", it.next()); // prints "
580 "`Some(&2)` printfln!(\"%?\", it.next_back()); // prints `Some(&6)`"
581 msgstr ""
582
583 #. type: Plain text
584 #: doc/tutorial-container.md:306
585 #, no-wrap
586 msgid ""
587 "// prints `5`, `4` and `3`\n"
588 "for &x in it.invert() {\n"
589 "    printfln!(\"%?\", x)\n"
590 "}\n"
591 "~~~\n"
592 msgstr ""
593
594 #. type: Plain text
595 #: doc/tutorial-container.md:309
596 msgid ""
597 "The `rev_iter` and `mut_rev_iter` methods on vectors just return an inverted "
598 "version of the standard immutable and mutable vector iterators."
599 msgstr ""
600
601 #. type: Plain text
602 #: doc/tutorial-container.md:312
603 msgid ""
604 "The `chain_`, `transform`, `filter`, `filter_map` and `peek` adaptors are "
605 "`DoubleEndedIterator` implementations if the underlying iterators are."
606 msgstr ""
607
608 #. type: Plain text
609 #: doc/tutorial-container.md:317
610 msgid ""
611 "~~~ let xs = [1, 2, 3, 4]; let ys = [5, 6, 7, 8]; let mut it = xs.iter()."
612 "chain_(ys.iter()).transform(|&x| x * 2);"
613 msgstr ""
614
615 #. type: Plain text
616 #: doc/tutorial-container.md:319
617 msgid "printfln!(\"%?\", it.next()); // prints `Some(2)`"
618 msgstr ""
619
620 #. type: Plain text
621 #: doc/tutorial-container.md:325
622 #, no-wrap
623 msgid ""
624 "// prints `16`, `14`, `12`, `10`, `8`, `6`, `4`\n"
625 "for x in it.invert() {\n"
626 "    printfln!(\"%?\", x);\n"
627 "}\n"
628 "~~~\n"
629 msgstr ""
630
631 #. type: Plain text
632 #: doc/tutorial-container.md:327
633 msgid "## Random-access iterators"
634 msgstr ""
635
636 #. type: Plain text
637 #: doc/tutorial-container.md:331
638 msgid ""
639 "The `RandomAccessIterator` trait represents an iterator offering random "
640 "access to the whole range. The `indexable` method retrieves the number of "
641 "elements accessible with the `idx` method."
642 msgstr ""
643
644 #. type: Plain text
645 #: doc/tutorial-container.md:334
646 msgid ""
647 "The `chain_` adaptor is an implementation of `RandomAccessIterator` if the "
648 "underlying iterators are."
649 msgstr ""
650
651 #. type: Plain text
652 #: doc/tutorial-container.md:343
653 msgid ""
654 "~~~ let xs = [1, 2, 3, 4, 5]; let ys = ~[7, 9, 11]; let mut it = xs.iter()."
655 "chain_(ys.iter()); printfln!(\"%?\", it.idx(0)); // prints `Some(&1)` "
656 "printfln!(\"%?\", it.idx(5)); // prints `Some(&7)` printfln!(\"%?\", it."
657 "idx(7)); // prints `Some(&11)` printfln!(\"%?\", it.idx(8)); // prints `None`"
658 msgstr ""
659
660 #. type: Plain text
661 #: doc/tutorial-container.md:348
662 msgid ""
663 "// yield two elements from the beginning, and one from the end it.next(); it."
664 "next(); it.next_back();"
665 msgstr ""
666
667 #. type: Plain text
668 #: doc/tutorial-container.md:352
669 msgid ""
670 "printfln!(\"%?\", it.idx(0)); // prints `Some(&3)` printfln!(\"%?\", it."
671 "idx(4)); // prints `Some(&9)` printfln!(\"%?\", it.idx(6)); // prints `None` "
672 "~~~"
673 msgstr ""