方法 `into_iter` 存在于结构体 `String` 中,但其特质限制未满足。

huangapple go评论72阅读模式
英文:

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&lt;u8&gt; = name
        .unwrap()
        .into_iter()
        .chain(online_presence.unwrap())
        .chain(requestor.to_keyed_vec(Default::default()))
        .chain(Self::env().block_timestamp().to_be_bytes())
        .collect();

方法 `into_iter` 存在于结构体 `String` 中,但其特质限制未满足。

error[E0599]: the method `into_iter` exists for struct `String`, but its trait bounds were not satisfied
   --&gt; /Users/ganesholi/Developer/me-protocol/rusty-protocol-v0.1/contracts/providers/services/brands.rs:63:14
    |
61  |           let concat: Vec&lt;u8&gt; = 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!(&quot;{}{}{}{}&quot;, 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::&lt;Vec&lt;_&gt;&gt;();

huangapple
  • 本文由 发表于 2023年6月26日 12:14:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76553475.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定