Jenny M
Jenny M

Reputation: 1023

Split value on array rust

I need to split value of array like

let v: Vec<&str> = token.split("Bearer ").collect();

and log the value of the token the value is Bearer eyJGciOiJSU and I need to get only eyJGciOiJSU when I print the v[1] to the log I got error index out of range, any idea?

Upvotes: 0

Views: 361

Answers (1)

isaactfa
isaactfa

Reputation: 6651

Hard to assess without a minimal example, but this works on the playground:

fn main() {
    let token = "Bearer eyJGciOiJSU";
    let v: Vec<_> = token.split("Bearer ").collect();
    println!("{}", v[1]); // prints "eyJGciOiJSU"
    
    let id = token.strip_prefix("Bearer ").expect(r#"Token ought to start with "Bearer""#);
    println!("{}", id); // prints "eyJGciOiJSU"
}

Upvotes: 4

Related Questions