Skip to content

Instantly share code, notes, and snippets.

@hyone
Last active January 1, 2025 20:12
Show Gist options
  • Save hyone/d6018ee1ac8f9496fed839f481eb59d6 to your computer and use it in GitHub Desktop.
Save hyone/d6018ee1ac8f9496fed839f481eb59d6 to your computer and use it in GitHub Desktop.

Revisions

  1. hyone revised this gist Oct 2, 2016. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions gistfile1.rs
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@ struct Slice<'a, T: 'a> {
    impl <'a, T: ToString> Display for Slice<'a, T> {
    fn fmt(&self, f: &mut Formatter) -> Result {
    let xs: Vec<_> = self.data.into_iter().map(|i| i.to_string()).collect();
    write!(f, "[{}]", xs.join(","))
    write!(f, "[{}]", xs.join(", "))
    }
    }

    @@ -24,9 +24,9 @@ fn main() {
    let vec: Vec<_> = (1..11).collect();

    double_prints(&Slice { data: &array });
    //=> Debug: `Slice { data: [1, 2, 3] }`
    // Display: `[1,2,3]`
    //=> Debug: `Slice { data: ["hello", "world", "rust", "programming"] }`
    // Display: `[hello, world, rust, programming]`
    double_prints(&Slice { data: &vec })
    //=> Debug: `Slice { data: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] }`
    // Display: `[1,2,3,4,5,6,7,8,9,10]`
    // Display: `[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]`
    }
  2. hyone revised this gist Oct 1, 2016. 1 changed file with 6 additions and 6 deletions.
    12 changes: 6 additions & 6 deletions gistfile1.rs
    Original file line number Diff line number Diff line change
    @@ -20,13 +20,13 @@ fn double_prints<T: Debug + Display>(t: &T) {


    fn main() {
    let array1 = [1, 2, 3];
    let array2 = ["hello", "world", "rust", "programming"];
    let array = ["hello", "world", "rust", "programming"];
    let vec: Vec<_> = (1..11).collect();

    double_prints(&Slice { data: &array1 });
    double_prints(&Slice { data: &array });
    //=> Debug: `Slice { data: [1, 2, 3] }`
    // Display: `[1,2,3]`
    double_prints(&Slice { data: &array2 })
    //=> Debug: `Slice { data: ["hello", "world", "rust", "programming"] }`
    // Display: `[hello,world,rust,programming]`
    double_prints(&Slice { data: &vec })
    //=> Debug: `Slice { data: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] }`
    // Display: `[1,2,3,4,5,6,7,8,9,10]`
    }
  3. hyone revised this gist Oct 1, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.rs
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@ struct Slice<'a, T: 'a> {
    data: &'a [T]
    }

    impl <'a, T: 'a + ToString> Display for Slice<'a, T> {
    impl <'a, T: ToString> Display for Slice<'a, T> {
    fn fmt(&self, f: &mut Formatter) -> Result {
    let xs: Vec<_> = self.data.into_iter().map(|i| i.to_string()).collect();
    write!(f, "[{}]", xs.join(","))
  4. hyone revised this gist Oct 1, 2016. No changes.
  5. hyone revised this gist Oct 1, 2016. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions gistfile1.rs
    Original file line number Diff line number Diff line change
    @@ -24,5 +24,9 @@ fn main() {
    let array2 = ["hello", "world", "rust", "programming"];

    double_prints(&Slice { data: &array1 });
    //=> Debug: `Slice { data: [1, 2, 3] }`
    // Display: `[1,2,3]`
    double_prints(&Slice { data: &array2 })
    //=> Debug: `Slice { data: ["hello", "world", "rust", "programming"] }`
    // Display: `[hello,world,rust,programming]`
    }
  6. hyone renamed this gist Oct 1, 2016. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  7. hyone created this gist Oct 1, 2016.
    28 changes: 28 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    use std::fmt::{ Debug, Display, Formatter, Result };
    use std::string::ToString;

    #[derive(Debug)]
    struct Slice<'a, T: 'a> {
    data: &'a [T]
    }

    impl <'a, T: 'a + ToString> Display for Slice<'a, T> {
    fn fmt(&self, f: &mut Formatter) -> Result {
    let xs: Vec<_> = self.data.into_iter().map(|i| i.to_string()).collect();
    write!(f, "[{}]", xs.join(","))
    }
    }

    fn double_prints<T: Debug + Display>(t: &T) {
    println!("Debug: `{:?}`", t);
    println!("Display: `{}`", t);
    }


    fn main() {
    let array1 = [1, 2, 3];
    let array2 = ["hello", "world", "rust", "programming"];

    double_prints(&Slice { data: &array1 });
    double_prints(&Slice { data: &array2 })
    }