Hello friends. I am working with the differ crate and wasm-bindgen's JsValue type.
The docs for Differ::new say I need to pass a slice, &'a [T]
for the strings to diff.
I thought I understood slices, until I tried:
fn get_strings(textA: JsValue, textB, JsValue) {
let str_a = textA.as_string().unwrap();
let str_b = textB.as_string().unwrap();
let string_slice_a = &str_a[..];
let string_slice_b = &str_b[..];
Differ::new(&slice_a, &slice_b); // mismatched types, expected slice, found &str
}
I thought you could generate a string slice from a String. In the differ docks, they point to taking &'a [T]
. Isn't that a slice? Is a string slice not the same as &[T]
?
This is how I understand them:
let s = "hello"; // &str
let slice_s = &s[..]; // string slice, which is also just a &str
let string_s = String::from("hello"); // String
let slice_string = &string_s[..]; // also a string slice
Any help on distinguishing slices and &str would be very helpful. Especially in this Differ context.
Thank you.