Skip to content

Commit 4ad76e6

Browse files
committed
Add example for str::subslice_offset
1 parent 9a16179 commit 4ad76e6

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/libcore/str.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2056,6 +2056,18 @@ pub fn as_buf<T>(s: &str, f: &fn(*u8, uint) -> T) -> T {
20562056

20572057
/**
20582058
* Returns the byte offset of an inner slice relative to an enclosing outer slice
2059+
*
2060+
* # Example
2061+
*
2062+
* ~~~
2063+
* let string = "a\nb\nc";
2064+
* let mut lines = ~[];
2065+
* for each_line(string) |line| { lines.push(line) }
2066+
*
2067+
* assert!(subslice_offset(string, lines[0]) == 0); // &"a"
2068+
* assert!(subslice_offset(string, lines[1]) == 2); // &"b"
2069+
* assert!(subslice_offset(string, lines[2]) == 4); // &"c"
2070+
* ~~~
20592071
*/
20602072
#[inline(always)]
20612073
pub fn subslice_offset(outer: &str, inner: &str) -> uint {
@@ -3461,6 +3473,13 @@ mod tests {
34613473
let c = slice(a, 0, len(a) - 6);
34623474
assert!(subslice_offset(a, b) == 7);
34633475
assert!(subslice_offset(a, c) == 0);
3476+
3477+
let string = "a\nb\nc";
3478+
let mut lines = ~[];
3479+
for each_line(string) |line| { lines.push(line) }
3480+
assert!(subslice_offset(string, lines[0]) == 0);
3481+
assert!(subslice_offset(string, lines[1]) == 2);
3482+
assert!(subslice_offset(string, lines[2]) == 4);
34643483
}
34653484
34663485
#[test]

0 commit comments

Comments
 (0)