What would this JS code look like in Rust: const capitalize = (name) => name.charAt(0).toUpperCase() + name.slice(1);
user
Sure, here's a Rust implementation of the same code:

```rust
fn capitalize(name: &str) -> String {
    format!("{}", name.char_at(0).to_ascii_uppercase() + name.trim_chars(|c| !c.is_ascii_alphabetic()))
}

fn main() {
    let name = "Hello World";
    println!("{:?}", capitalize(name)); // Output: "Hello World"
}
```

Here, `format!` is used to concatenate `to_ascii_uppercase` and `trim_chars` output. The `trim_chars` method removes characters other than ASCII alphabets from the string. This ensures that if the input string contains any Unicode characters, only the first letter of the original string will be uppercased.
assistant
What would this JS code look like in Rust: const capitalize = (name) => name.charAt(0).toUpperCase() + name.slice(1);
user
In Rust, you could write a function `capitalize` that takes a string and returns a new string with the first character capitalized like this:

```rust
fn capitalize(name: &str) -> String {
    let mut c = name.chars();
    match c.next() {
        None => String::new(),
        Some(f) => f.to_uppercase().chain(c).collect(),
    }
}
```

Here's how this Rust function works:

1. The function takes a string slice (`&str`) as an argument and returns a new `String`.
2. It creates a mutable iterator `c` over the characters (`chars()`) of the input string.
3. Using `match` statement, it checks the first character of the iterator:
    - If there is no character (`None`), it returns an empty string.
    - If there is a character (`Some(f)`), it converts it to uppercase (`to_uppercase()`), chains the rest of the characters from the iterator (`chain(c)`), and collects the results into a new string (`collect()`).

To use this function, you can do:

```rust
let name = "john";
let capitalized_name = capitalize(name);
println!("{}", capitalized_name); // Output: John
```

This Rust function has the same effect as the JavaScript function `const capitalize = (name) => name.charAt(0).toUpperCase() + name.slice(1);`.
assistant