@@ -453,15 +453,15 @@ public struct IndexingIterator<
453
453
///
454
454
/// let text = "Buffalo buffalo buffalo buffalo."
455
455
/// if let firstSpace = text.index(of: " ") {
456
- /// print(text.prefix(upTo: firstSpace) )
456
+ /// print(text[..< firstSpace] )
457
457
/// }
458
458
/// // Prints "Buffalo"
459
459
///
460
- /// The `firstSpace` constant is an index into the `text` string---the position of the first space in the
461
- /// string. You can store indices in variables, and pass them to
462
- /// collection algorithms or use them later to access the corresponding
463
- /// element. In the example above, `firstSpace` is used to extract the prefix
464
- /// that contains elements up to that index.
460
+ /// The `firstSpace` constant is an index into the `text` string---the position
461
+ /// of the first space in the string. You can store indices in variables, and
462
+ /// pass them to collection algorithms or use them later to access the
463
+ /// corresponding element. In the example above, `firstSpace` is used to
464
+ /// extract the prefix that contains elements up to that index.
465
465
///
466
466
/// You can pass only valid indices to collection operations. You can find a
467
467
/// complete set of a collection's valid indices by starting with the
@@ -514,20 +514,18 @@ public struct IndexingIterator<
514
514
/// // Prints "Buffalo"
515
515
///
516
516
/// You can retrieve the same slice using other methods, such as finding the
517
- /// terminating index, and then using the `prefix(upTo:)` method, which takes
518
- /// an index as its parameter, or by using the view's ranged subscript .
517
+ /// terminating index, and the using the string's ranged subscript, or by
518
+ /// using the `prefix(upTo:)` method, which takes an index as its parameter .
519
519
///
520
520
/// if let firstSpace = text.index(of: " ") {
521
- /// print(text.prefix(upTo: firstSpace))
521
+ /// print(text[..< firstSpace]
522
522
/// // Prints "Buffalo"
523
523
///
524
- /// let start = text.startIndex
525
- /// print(text[start..<firstSpace])
524
+ /// print(text.prefix(upTo: firstSpace))
526
525
/// // Prints "Buffalo"
527
526
/// }
528
527
///
529
- /// The retrieved slice of `text` is equivalent in each of these
530
- /// cases.
528
+ /// The retrieved slice of `text` is equivalent in each of these cases.
531
529
///
532
530
/// Slices Share Indices
533
531
/// --------------------
@@ -795,6 +793,15 @@ public protocol Collection : _Indexable, Sequence
795
793
/// print(numbers.prefix(upTo: numbers.startIndex))
796
794
/// // Prints "[]"
797
795
///
796
+ /// Using the `prefix(upTo:)` method is equivalent to using a partial
797
+ /// half-open range as the collection's subscript. The subscript notation is
798
+ /// preferred over `prefix(upTo:)`.
799
+ ///
800
+ /// if let i = numbers.index(of: 40) {
801
+ /// print(numbers[..<i])
802
+ /// }
803
+ /// // Prints "[10, 20, 30]"
804
+ ///
798
805
/// - Parameter end: The "past the end" index of the resulting subsequence.
799
806
/// `end` must be a valid index of the collection.
800
807
/// - Returns: A subsequence up to, but not including, the `end` position.
@@ -822,6 +829,15 @@ public protocol Collection : _Indexable, Sequence
822
829
/// print(numbers.suffix(from: numbers.endIndex))
823
830
/// // Prints "[]"
824
831
///
832
+ /// Using the `suffix(from:)` method is equivalent to using a partial range
833
+ /// from the index as the collection's subscript. The subscript notation is
834
+ /// preferred over `suffix(from:)`.
835
+ ///
836
+ /// if let i = numbers.index(of: 40) {
837
+ /// print(numbers[i...])
838
+ /// }
839
+ /// // Prints "[40, 50, 60]"
840
+ ///
825
841
/// - Parameter start: The index at which to start the resulting subsequence.
826
842
/// `start` must be a valid index of the collection.
827
843
/// - Returns: A subsequence starting at the `start` position.
@@ -843,6 +859,15 @@ public protocol Collection : _Indexable, Sequence
843
859
/// }
844
860
/// // Prints "[10, 20, 30, 40]"
845
861
///
862
+ /// Using the `prefix(through:)` method is equivalent to using a partial
863
+ /// closed range as the collection's subscript. The subscript notation is
864
+ /// preferred over `prefix(through:)`.
865
+ ///
866
+ /// if let i = numbers.index(of: 40) {
867
+ /// print(numbers[...i])
868
+ /// }
869
+ /// // Prints "[10, 20, 30, 40]"
870
+ ///
846
871
/// - Parameter end: The index of the last element to include in the
847
872
/// resulting subsequence. `end` must be a valid index of the collection
848
873
/// that is not equal to the `endIndex` property.
@@ -1619,6 +1644,15 @@ extension Collection {
1619
1644
/// print(numbers.prefix(upTo: numbers.startIndex))
1620
1645
/// // Prints "[]"
1621
1646
///
1647
+ /// Using the `prefix(upTo:)` method is equivalent to using a partial
1648
+ /// half-open range as the collection's subscript. The subscript notation is
1649
+ /// preferred over `prefix(upTo:)`.
1650
+ ///
1651
+ /// if let i = numbers.index(of: 40) {
1652
+ /// print(numbers[..<i])
1653
+ /// }
1654
+ /// // Prints "[10, 20, 30]"
1655
+ ///
1622
1656
/// - Parameter end: The "past the end" index of the resulting subsequence.
1623
1657
/// `end` must be a valid index of the collection.
1624
1658
/// - Returns: A subsequence up to, but not including, the `end` position.
@@ -1649,6 +1683,15 @@ extension Collection {
1649
1683
/// print(numbers.suffix(from: numbers.endIndex))
1650
1684
/// // Prints "[]"
1651
1685
///
1686
+ /// Using the `suffix(from:)` method is equivalent to using a partial range
1687
+ /// from the index as the collection's subscript. The subscript notation is
1688
+ /// preferred over `suffix(from:)`.
1689
+ ///
1690
+ /// if let i = numbers.index(of: 40) {
1691
+ /// print(numbers[i...])
1692
+ /// }
1693
+ /// // Prints "[40, 50, 60]"
1694
+ ///
1652
1695
/// - Parameter start: The index at which to start the resulting subsequence.
1653
1696
/// `start` must be a valid index of the collection.
1654
1697
/// - Returns: A subsequence starting at the `start` position.
@@ -1673,6 +1716,15 @@ extension Collection {
1673
1716
/// }
1674
1717
/// // Prints "[10, 20, 30, 40]"
1675
1718
///
1719
+ /// Using the `prefix(through:)` method is equivalent to using a partial
1720
+ /// closed range as the collection's subscript. The subscript notation is
1721
+ /// preferred over `prefix(through:)`.
1722
+ ///
1723
+ /// if let i = numbers.index(of: 40) {
1724
+ /// print(numbers[...i])
1725
+ /// }
1726
+ /// // Prints "[10, 20, 30, 40]"
1727
+ ///
1676
1728
/// - Parameter end: The index of the last element to include in the
1677
1729
/// resulting subsequence. `end` must be a valid index of the collection
1678
1730
/// that is not equal to the `endIndex` property.
0 commit comments