Dear mighty Rustaceans,
I'm newbie in the Rust world and having trouble with joining byte strings.
Here's my code snippet that I expected to work.
fn main() {
assert_eq!(["next", "gen"].join(" "), "next gen"); // work
assert_eq!(vec![b"this", b"work"].as_slice().join(b" "), b"this work"); // this doesn't complain for creating vec
assert_eq!(vec![&b"next", &b"gen"].as_slice().join(&b" "), b"next gen"); // bad
assert_eq!(vec![b"next", b"gen"].as_slice().join(b" "), b"next gen"); // bad
assert_eq!([b"next", b"gen"].join(&b" "), b"next gen"); // bad
}
And I'm seeing following compile errors.
Compiling playground v0.0.1 (/playground)
error[E0599]: the method `join` exists for reference `&[&[u8; 4]]`, but its trait bounds were not satisfied
--> src/main.rs:3:50
|
3 | assert_eq!(vec![b"this", b"work"].as_slice().join(b" "), b"this work"); // this doesn't complain for creating vec
| ^^^^ method cannot be called on `&[&[u8; 4]]` due to unsatisfied trait bounds
|
= note: the following trait bounds were not satisfied:
`[&[u8; 4]]: Join<_>`
error[E0308]: mismatched types
--> src/main.rs:5:31
|
5 | assert_eq!(vec![&b"next", &b"gen"].as_slice().join(&b" "), b"next gen"); // bad
| ^^^^^^^ expected an array with a fixed size of 4 elements, found one with 3 elements
error[E0599]: the method `join` exists for reference `&[&&[u8; 4]]`, but its trait bounds were not satisfied
--> src/main.rs:5:51
|
5 | assert_eq!(vec![&b"next", &b"gen"].as_slice().join(&b" "), b"next gen"); // bad
| ^^^^ method cannot be called on `&[&&[u8; 4]]` due to unsatisfied trait bounds
|
= note: the following trait bounds were not satisfied:
`[&&[u8; 4]]: Join<_>`
error[E0308]: mismatched types
--> src/main.rs:6:30
|
6 | assert_eq!(vec![b"next", b"gen"].as_slice().join(b" "), b"next gen"); // bad
| ^^^^^^ expected an array with a fixed size of 4 elements, found one with 3 elements
error[E0599]: the method `join` exists for reference `&[&[u8; 4]]`, but its trait bounds were not satisfied
--> src/main.rs:6:49
|
6 | assert_eq!(vec![b"next", b"gen"].as_slice().join(b" "), b"next gen"); // bad
| ^^^^ method cannot be called on `&[&[u8; 4]]` due to unsatisfied trait bounds
|
= note: the following trait bounds were not satisfied:
`[&[u8; 4]]: Join<_>`
error[E0308]: mismatched types
--> src/main.rs:7:26
|
7 | assert_eq!([b"next", b"gen"].join(&b" "), b"next gen"); // bad
| ^^^^^^ expected an array with a fixed size of 4 elements, found one with 3 elements
Some errors have detailed explanations: E0308, E0599.
For more information about an error, try `rustc --explain E0308`.
error: could not compile `playground` due to 6 previous errors
String version of my code worlds, but I can't make it with the byte strings.
First of all, I don't understand why does the vec
expect the same sized byte strings.
Isn't byte strings slice?
How can instantiate vector with variable sized byte strings, without extra allocation?
Secondly, why does the join
complain about trait bounds?
I read the documentation of slices
and join
was there. se:: slice - Rust
Isn't it enough for byte string [u8]
to support?
It seems that I'm misunderstanding some basic, and I'm asking for help.
Can someone help me? It would be a great help.
Sincerely,