小编KDN*_*KDN的帖子

如何同时声明多个可变变量?

我可以像这样声明多个常量:

let (a, b, c) = (1, 0.0, 3);
Run Code Online (Sandbox Code Playgroud)

但为什么我不能用可变变量做这个呢?

let mut (a, b, c) = (1, 0.0, 3); 抛出编译错误:

error: expected identifier, found `(`
 --> <anon>:2:13
2 |>     let mut (a, b, c) = (1, 0.0, 3);
  |>             ^
Run Code Online (Sandbox Code Playgroud)

rust

13
推荐指数
1
解决办法
1859
查看次数

Rust-racer 的货物路径设置

我刚刚安装racer使用cargo. 安装后说:

Installing /home/karthik/.cargo/bin/racer
warning: be sure to add `/home/karthik/.cargo/bin` to your PATH to be able to run the installed binaries
Run Code Online (Sandbox Code Playgroud)

我该怎么做呢?谷歌搜索没有帮助。另外,我也应该PATH为货箱设置一个变量吗?

编辑:操作系统是 Ubuntu 14.04,我有超级用户访问权限

rust rust-cargo

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

无法创建矢量并将其随机播放

我正在尝试创建一个数字为48到57的向量,然后随机地将其洗牌.我遇到了以下错误

error: the type of this value must be known in this context
        let &mut slice = secret_num.as_mut_slice();
                         ^~~~~~~~~~~~~~~~~~~~~~~~~
error: no method named `shuffle` found for type `rand::ThreadRng` in the current scope
        rng.shuffle(&mut slice);
            ^~~~~~~
Run Code Online (Sandbox Code Playgroud)

这是代码:

extern crate rand;

fn main() {
    //Main game loop
    loop{
        let mut secret_num = (48..58).collect();
        let &mut slice = secret_num.as_mut_slice();
        let mut rng = rand::thread_rng();
        rng.shuffle(&mut slice);                                            
        println!("{:?}", secret_num);
        break;
    }
    println!("Hello, world!");
}
Run Code Online (Sandbox Code Playgroud)

random rust

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

为什么我的素数筛子包括49号?

我无法在筛网实施中发现错误.我的测试显示以下错误.

// Expected 
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
// Mine
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 49]
Run Code Online (Sandbox Code Playgroud)

有人可以指出我做错了吗?我意识到这与最终条件有关,但我无法弄明白

/// Find all prime numbers less than `n`.
/// For example, `sieve(7)` should return `[2, 3, 5]`
pub fn sieve(n: u32) -> Vec<u32> {
    // Check assert and then populate vector
    assert!(n > 1, "Error n is less than 1");
    let mut is_prime: …
Run Code Online (Sandbox Code Playgroud)

rust

0
推荐指数
1
解决办法
116
查看次数

数字条件下的锈匹配

我有以下匹配条件,测试输入的猜测是否是有效数字(来自铁锈猜谜游戏练习):

let mut guess = String::new(); 
io::stdin().read_line(&mut guess).expect("Failed to read");
let guess: u32 = match guess.trim().parse() {
    Ok(num) => num,
    Err(_)  => continue,
};
Run Code Online (Sandbox Code Playgroud)

如何扩展匹配以检查数字是否为负数或0(我想在这种情况下抛出错误)并在数字为正数时正常进行?

我尝试在Ok case中的num上添加一个if,但是会抛出一个错误(可能是因为num绑定到guess的值)并且似乎也不是惯用的.

pattern-matching rust

0
推荐指数
1
解决办法
1629
查看次数

通过 Rust FFI 更改 C 数组元素

我正在使用 Rust 来测试一些 C 代码:

哈哈

#include "lol.h"
int a[10]; //Assume lol.h has an extern declaration for a[10]
Run Code Online (Sandbox Code Playgroud)

库文件

extern "C" {
    static a: *mut i32;
}

fn set_a(val: i32, index: usize) {
    assert!(index < 10);
    unsafe {
        a[index] = val;
    }
}

fn get_a(index: usize) {
    assert!(index < 10);
    unsafe { a[index] }
}
Run Code Online (Sandbox Code Playgroud)

我使用cc crate来编译和链接 lol.o。set_aget_a函数怎么写?编译器说:

extern "C" {
    static a: *mut i32;
}

fn set_a(val: i32, index: usize) {
    assert!(index < …
Run Code Online (Sandbox Code Playgroud)

ffi rust

0
推荐指数
1
解决办法
800
查看次数

标签 统计

rust ×6

ffi ×1

pattern-matching ×1

random ×1

rust-cargo ×1