小编Nig*_*eXD的帖子

当无法使 main 函数异步时如何在 Rust 中使用 async/await

我有一个下载异步文件的函数:

async fn download_file() {
    fn main() {
        let resp = reqwest::blocking::get("https://sh.rustup.rs").expect("request failed");
        let body = resp.text().expect("body invalid");
        let mut out = File::create("rustup-init.sh").expect("failed to create file");
        io::copy(&mut body.as_bytes(), &mut out).expect("failed to copy content");
    }
}
Run Code Online (Sandbox Code Playgroud)

我想调用这个函数来下载文件,然后在需要时等待它。
但问题是,如果我这样做,我会得到一个错误:

fn main() {
    let download = download_file();
    // Do some work
    download.await; // `await` is only allowed inside `async` functions and blocks\nonly allowed inside `async` functions and blocks
    // Do some work
}
Run Code Online (Sandbox Code Playgroud)

所以我必须使 main 函数异步,但是当我这样做时,我收到另一个错误:

async fn main() { // `main` function …
Run Code Online (Sandbox Code Playgroud)

rust async-await

24
推荐指数
2
解决办法
2万
查看次数

如何指定 for 循环中循环变量的类型?

i我想指定这个 for 循环中的类型。Visual Studio Code 告诉我输入的类型,i32但数字不是那么大,所以我想将其更改为u8.

for i in 0..101 {
    println!("{}", i);
}
Run Code Online (Sandbox Code Playgroud)

我试过这个:

for i: u8 in 0..101 {
    println!("{}", i);
}
Run Code Online (Sandbox Code Playgroud)

但是,我收到此错误:

error: expected one of `@` or `|`, found `:`
 --> src/main.rs:2:10
  |
2 |     for i: u8 in 0..101 {
  |          ^
  |          |
  |          expected one of `@` or `|`
  |          help: maybe write a path separator here: `::`
Run Code Online (Sandbox Code Playgroud)

指定类型的正确方法是什么?

rust

6
推荐指数
1
解决办法
1181
查看次数

为什么 Rust 标准库有这么多不安全代码?

我在标准库中查看了String很多unsafe这样的代码:

    #[inline]
    #[stable(feature = "rust1", since = "1.0.0")]
    pub fn remove(&mut self, idx: usize) -> char {
        let ch = match self[idx..].chars().next() {
            Some(ch) => ch,
            None => panic!("cannot remove a char from the end of a string"),
        };

        let next = idx + ch.len_utf8();
        let len = self.len();
        unsafe {
            ptr::copy(self.vec.as_ptr().add(next), self.vec.as_mut_ptr().add(idx), len - next);
            self.vec.set_len(len - (next - idx));
        }
        ch
    }
Run Code Online (Sandbox Code Playgroud)

unsafe为什么标准库里有这么多代码?语言如何仍然安全?

unsafe rust

3
推荐指数
1
解决办法
1136
查看次数

如果在 C++ 中内联一个调用 self 的函数会发生什么

首先,我认为编译时间会永远持续下去,或者我遇到一个奇怪的错误,但这并没有发生。代码运行了一段时间然后崩溃了。

这是我的代码:

#include <iostream>

inline void say_hello()
{
    std::cout << "hello\n";
    say_hello();
}

int main()
{
    say_hello();
}

Run Code Online (Sandbox Code Playgroud)

我以为编译器会把它转换成这样的:

#include <iostream>

int main()
{
    std::cout << "hello\n";
    std::cout << "hello\n";
    std::cout << "hello\n";
    std::cout << "hello\n";
    std::cout << "hello\n";
    std::cout << "hello\n";
    std::cout << "hello\n";
    std::cout << "hello\n";
    std::cout << "hello\n";
    std::cout << "hello\n";
    std::cout << "hello\n";
    std::cout << "hello\n";
    std::cout << "hello\n";
    std::cout << "hello\n";
    std::cout << "hello\n";
    // and crash because my storage is filled
}
Run Code Online (Sandbox Code Playgroud)

但是,我认为编译器忽略了该inline …

c++ visual-c++

2
推荐指数
1
解决办法
191
查看次数

标签 统计

rust ×3

async-await ×1

c++ ×1

unsafe ×1

visual-c++ ×1