为什么嵌套的关联类型路径被认为是不明确的?
fn flatten<I>(iter: I) -> Option<I::Item::Item>
where
I: Iterator,
I::Item: IntoIterator,
{
None
}
Run Code Online (Sandbox Code Playgroud)
error[E0223]: ambiguous associated type
--> src/lib.rs:1:34
|
1 | fn flatten<I>(iter: I) -> Option<I::Item::Item>
| ^^^^^^^^^^^^^ help: use fully-qualified syntax: `<<I as Iterator>::Item as Trait>::Item`
Run Code Online (Sandbox Code Playgroud)
编译器不应该能够在没有完全限定语法的情况下解析路径吗?我只能指一种类型,所以我不明白为什么它是模棱两可的。在这个简单的情况下,这不是真正的问题,但在更复杂的用例中,它变得很痛苦:
fn blah() -> <<<<A as B>::C as D>::E as F>::G as H>::I as J>::K> {}
Run Code Online (Sandbox Code Playgroud)
这是编译器的限制还是预期的行为?
我正在尝试在一个项目中使用柴油,并且我想要一个“可过滤”类型。这个想法是你可以去/api/foo?id=10&bar=11
它会返回一个 struct Foo
:
struct Foo {
id: Option<i64>,
bar: Option<i64>,
name: Option<String>,
}
Run Code Online (Sandbox Code Playgroud)
例如:
Foo {
id: Some(10),
bar: Some(11),
name: None,
}
Run Code Online (Sandbox Code Playgroud)
我一直在互联网上搜索一种按现有字段进行过滤的方法,但我无法找到有效的解决方案。我最初使用mysql 驱动程序并使用 proc 宏构建 sql 查询,但是diesel 更好用,我想知道是否有一种方法可以获得与柴油机 mysql 驱动程序相同的行为。
为什么在 trait 方法声明中允许使用 unsized 类型?例如,此代码编译:
trait Blah {
fn blah(&self, input: [u8]) -> dyn Display;
}
Run Code Online (Sandbox Code Playgroud)
但实施Blah
是不可能的:
impl Blah for Foo {
fn blah(&self, input: [u8]) -> dyn Display {
"".to_string()
}
}
// error[E0277]: the size for values of type `(dyn std::fmt::Display + 'static)` cannot be known at compilation time
// error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
Run Code Online (Sandbox Code Playgroud)
给出blah
默认实现也是不可能的:
trait Blah {
fn blah(&self, input: dyn Display) -> …
Run Code Online (Sandbox Code Playgroud) 我想知道是否可以enum
在 Rust 中使用常量字符串值创建?
我发现了上一个问题:如何将枚举作为字符串获取?它显示了一个解决方法,我可以用它来字符串化 的变体enum
(我可以.to_string()
在enum
变体上使用并将它们的名称作为字符串)。
这个问题很有帮助,但这就是我想要实现的目标:
enum StringEnum {
Hello = "Hello",
World = "World"
}
Run Code Online (Sandbox Code Playgroud) 我有一个特征,我用它来抽象tokio::net::TcpStream
和tokio::net::UnixStream
:
/// Interface for TcpStream and UnixStream.
trait TryRead {
// overlapping the name makes it hard to work with
fn do_try_read(&self, buf: &mut [u8]) -> Result<usize, std::io::Error>;
}
impl TryRead for TcpStream {
fn do_try_read(&self, buf: &mut [u8]) -> Result<usize, std::io::Error> {
self.try_read(buf)
}
}
Run Code Online (Sandbox Code Playgroud)
问题是我想pub async fn readable(&self) -> io::Result<()>
在这两种方法中都抽象出来,但是无法在特征中实现异步方法。我该如何处理?
我正在用 Rust 编写一个程序,涉及通过 TCP 连接发送数据。我无法弄清楚将结构转换为字节数组并返回的方法。其他解决方案只能将其转换为u8
,但由于我是 Rust 新手(只有 3 个月),我无法弄清楚。我希望你们能提供一种方法来做到这一点。
我正在尝试使用crates_io_api
. 我试图从流中获取数据,但我无法让它工作。
AsyncClient::all_crates
返回一个impl Stream
. 我如何从中获取数据?如果你提供代码会很有帮助。
我检查了异步书,但没有用。谢谢你。
这是我当前的代码。
use crates_io_api::{AsyncClient, Error};
use futures::stream::StreamExt;
async fn get_all(query: Option<String>) -> Result<crates_io_api::Crate, Error> {
// Instantiate the client.
let client = AsyncClient::new(
"test (test@test.com)",
std::time::Duration::from_millis(10000),
)?;
let stream = client.all_crates(query);
// what should I do after?
// ERROR: `impl Stream cannot be unpinned`
while let Some(item) = stream.next().await {
// ...
}
}
Run Code Online (Sandbox Code Playgroud) 如何更新状态数组中的单个元素?这是我目前使用的代码:
const Cars = props => {
const [cars, setCars] = React.useState(["Honda","Toyota","Dodge"])
const handleClick1 = () => { setCars[0]("Jeep") }
const handleClick2 = () => { setCars[1]("Jeep") }
const handleClick3 = () => { setCars[2]("Jeep") }
return (
<div>
<button onClick={handleClick1}>{cars[0]}</button>
<button onClick={handleClick2}>{cars[1]}</button>
<button onClick={handleClick3}>{cars[2]}</button>
</div>
)
};
Run Code Online (Sandbox Code Playgroud)
当我单击呈现的按钮之一时,我得到Uncaught TypeError: setCars[0] is not a function at handleClick1
.
我知道如何在 React 类中执行此操作,但是如何使用 React Hooks 执行此操作?
将 a 添加&str
到 a的最有效方法是String
什么?现在我正在分配一个新的String
并推动它:
fn push_front(s: String, prefix: &str) -> String {
let prefix = prefix.to_owned();
prefix.push_str(&s);
prefix
}
Run Code Online (Sandbox Code Playgroud)
有没有更有效的方法?我String::push_front
在标准库中没有看到函数。
我ngrok
在尝试找到一种通过 Android 设备连接到本地主机 API 的方法时偶然发现了这一点。
我有一个问题,使用ngrok
安全吗?如果不是,那么它有哪些威胁?
假设我在 Rust 中有一个具有以下签名的函数:
fn f<'a>(x: &'a i32) -> &'a i32;
Run Code Online (Sandbox Code Playgroud)
假设我执行以下操作:
let x = 0;
let y = f(&x);
Run Code Online (Sandbox Code Playgroud)
在这种情况下,Rust 借用检查器会考虑y
借用x
. 为什么?比“因为您在参数类型和返回类型中使用了相同的生命周期参数”更深层次的原因是什么?
考虑如下结构:
struct Foo;
Run Code Online (Sandbox Code Playgroud)
我想为此写一些文档,所以我这样做:
/// whatever
struct Foo;
Run Code Online (Sandbox Code Playgroud)
但是,当我按 Enter 时,它会创建一个空行:
/// whatever
struct Foo;
Run Code Online (Sandbox Code Playgroud)
不是在文档字符串中添加新行,而是///
在其前面添加:
/// whatever
///
struct Foo;
Run Code Online (Sandbox Code Playgroud)
我已经六个月没有碰电脑或编程了。我记得六个月前我就能做到。也许是因为 6 个月前我有一个扩展(也许是 RLS?)调用了该行为,但现在没有。
如何在 Rust 和 VSCode 中以换行符继续文档字符串?
VSCode 和扩展
其他
rust ×10
arrays ×2
backend ×1
byte ×1
enums ×1
javascript ×1
ngrok ×1
node.js ×1
react-native ×1
reactjs ×1
rust-diesel ×1
rust-futures ×1
rust-tokio ×1
state ×1
struct ×1
tcp ×1
typeerror ×1