bio/utils/
text.rs

1/// Type alias for an owned text, i.e. ``Vec<u8>``.
2pub type Text = Vec<u8>;
3/// Type alias for a text slice, i.e. ``&[u8]``.
4pub type TextSlice<'a> = &'a [u8];
5
6/// Remove a trailing newline from the given string in place.
7pub fn trim_newline(s: &mut String) {
8    if s.ends_with('\n') {
9        s.pop();
10    }
11}
12
13#[cfg(test)]
14mod tests {
15    use super::*;
16    use std::ops::Deref;
17
18    /// This function demonstrates the use of the IntoSequenceIterator alias, which takes both
19    /// slices and iterators.
20    fn print_sequence<Item: Deref<Target = u8>, T: IntoIterator<Item = Item>>(sequence: T) {
21        for c in sequence {
22            println!("{}", *c);
23        }
24    }
25
26    #[test]
27    fn test_print_sequence() {
28        let s = b"ACGT";
29        // use iterator
30        print_sequence(s.iter().step_by(1));
31        // use slice
32        print_sequence(&s[..]);
33        // use vec
34        print_sequence(&vec![b'A', b'C']);
35        // keep ownership
36        println!("{:?}", s);
37    }
38
39    #[test]
40    fn test_trim_newline_from_string() {
41        let mut s = String::from("AGCT\n");
42        trim_newline(&mut s);
43        assert_eq!(s, String::from("AGCT"));
44    }
45}