英文:
the method `into_iter` exists for struct `String`, but its trait bounds were not satisfied
问题
I want to concatenate the name
which is a String with online_presence
and requestor
, but I encountered this issue.
let concat: Vec<u8> = name
.unwrap()
.into_iter()
.chain(online_presence.unwrap())
.chain(requestor.to_keyed_vec(Default::default()))
.chain(Self::env().block_timestamp().to_be_bytes())
.collect();
Error message:
error[E0599]: the method `into_iter` exists for struct `String`, but its trait bounds were not satisfied
--> /Users/ganesholi/Developer/me-protocol/rusty-protocol-v0.1/contracts/providers/services/brands.rs:63:14
|
61 | let concat: Vec<u8> = name
| _______________________________-
62 | | .unwrap()
63 | | .into_iter()
| | ^^^^^^^^^^
Can somebody suggest how to resolve this issue?
英文:
I wan't to concat name which is String with online_presence and requestor but I got this issue.
let concat: Vec<u8> = name
.unwrap()
.into_iter()
.chain(online_presence.unwrap())
.chain(requestor.to_keyed_vec(Default::default()))
.chain(Self::env().block_timestamp().to_be_bytes())
.collect();
error[E0599]: the method `into_iter` exists for struct `String`, but its trait bounds were not satisfied
--> /Users/ganesholi/Developer/me-protocol/rusty-protocol-v0.1/contracts/providers/services/brands.rs:63:14
|
61 | let concat: Vec<u8> = name
| _______________________________-
62 | | .unwrap()
63 | | .into_iter()
Can somebody suggest do I resolve this issue?
答案1
得分: 4
听起来有点像XY问题。
如果我理解正确,你真正想做的事情似乎是将多个字符串连接在一起,而不会发生不必要的分配。
至少需要一次分配,然后你可以使用format!。你还可以使用write!来预先分配一个缓冲区。
let concat = format!("{}{}{}{}", name, online_presence.unwrap(), requestor.to_keyed_vec(Default::default()), Self::env().block_timestamp());
如果你不想得到一个字符串,而是想要链式连接特定的字节序列,请使用String::into_bytes,然后在其上调用into_iter
+ chain
:
let concat = name.into_bytes().into_iter()
.chain(online_presence.unwrap()) // 假设这已经是字节的迭代器
.chain(requestor.to_keyed_vec(Default::default())) // 假设这已经是字节的迭代器
.chain(Self::env().block_timestamp().to_be_bytes())
.collect::<Vec<_>>();
英文:
Sounds a bit like the XY Problem.
If I understand correctly, what you really want to do seems to be to concat multiple strings together without occurring unnecessary allocations.
You'll at least need one allocation, at which point you can just format!. You can also use write! to pre-allocate a buffer.
let concat = format!("{}{}{}{}", name, online_presence.unwrap(), requestor.to_keyed_vec(Default::default()), Self::env().block_timestamp());
If you don't want a string, but instead want to chain together certain byte sequences use String::into_bytes and call into_iter
+ chain
on that:
let concat = name.into_bytes().into_iter()
.chain(online_presence.unwrap()) // assuming this already is an iter over bytes
.chain(requestor.to_keyed_vec(Default::default())) // assuming this already is an iter over bytes
.chain(Self::env().block_timestamp().to_be_bytes())
.collect::<Vec<_>>();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论