Skip to content
TheChatArchiveThe Chat Archive
Claudeclaude-sonnet-4-20250514-thinking-32k· AnthropicHugging Face LMArena Human Preference 140K

Write a simple rust function that takes a polymorphic type and returns a String of that…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Coherecommand-a-03-2025’s answer.
  1. User

    Write a simple rust function that takes a polymorphic type and returns a String of that type.

  2. Claude Assistant

    Here's a simple Rust function that takes a polymorphic type and returns a String:

    fn to_string_display<T: std::fmt::Display>(value: T) -> String {
        value.to_string()
    }
    

    Or if you want to use the Debug trait instead (which is more commonly implemented):

    fn to_string_debug<T: std::fmt::Debug>(value: T) -> String {
        format!("{:?}", value)
    }
    

    Here's how you'd use them:

    fn main() {
        // Using Display version
        let number = to_string_display(42);
        let text = to_string_display("hello");
        
        println!("{}", number); // "42"
        println!("{}", text);   // "hello"
        
        // Using Debug version
        let vec = to_string_debug(vec![1, 2, 3]);
        let tuple = to_string_debug((1, "test"));
        
        println!("{}", vec);   // "[1, 2, 3]"
        println!("{}", tuple); // "(1, \"test\")"
    }
    

    The Display version works with types that have a user-friendly string representation, while the Debug version works with almost any type since most types can derive or implement Debug.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.