2015-03-29 12 views
13

Ich versuchteWie kann ich in Rust 1.0 eine Zufallszahl erhalten?

use std::rand::{task_rng, Rng}; 

fn main() { 
    // a number from [-40.0, 13000.0) 
    let num: f64 = task_rng().gen_range(-40.0, 1.3e4); 
    println!("{}", num); 
} 

aber das gibt

error[E0432]: unresolved import `std::rand::task_rng` 
--> rand.rs:1:17 
    | 
1 | use std::rand::{task_rng, Rng}; 
    |     ^^^^^^^^ no `task_rng` in `rand` 

error[E0432]: unresolved import `std::rand::Rng` 
--> rand.rs:1:27 
    | 
1 | use std::rand::{task_rng, Rng}; 
    |       ^^^ no `Rng` in `rand` 

error[E0603]: module `rand` is private 
--> rand.rs:1:17 
    | 
1 | use std::rand::{task_rng, Rng}; 
    |     ^^^^^^^^ 

error[E0603]: module `rand` is private 
--> rand.rs:1:27 
    | 
1 | use std::rand::{task_rng, Rng}; 
    |       ^^^ 

und ich versuchte

extern crate rand; 
use rand::Rng; 

fn main() { 
    let mut rng = rand::thread_rng(); 
    if rng.gen() { 
     // random bool 
     println!("i32: {}, u32: {}", rng.gen::<i32>(), rng.gen::<u32>()) 
    } 
    let tuple = rand::random::<(f64, char)>(); 
    println!("{:?}", tuple) 
} 

und bekam

error[E0425]: cannot find function `thread_rng` in module `rand` 
--> rand.rs:5:29 
    | 
5 |   let mut rng = rand::thread_rng(); 
    |        ^^^^^^^^^^ not found in `rand` 
    | 
help: possible candidate is found in another module, you can import it into scope 
    |  use std::__rand::thread_rng; 

error[E0425]: cannot find function `random` in module `rand` 
    --> rand.rs:10:27 
    | 
10 |   let tuple = rand::random::<(f64, char)>(); 
    |       ^^^^^^ not found in `rand` 

error: use of unstable library feature 'rand': use `rand` from crates.io (see issue #27703) 
--> rand.rs:1:5 
    | 
1 |  extern crate rand; 
    |  ^^^^^^^^^^^^^^^^^^ 

error: use of unstable library feature 'rand': use `rand` from crates.io (see issue #27703) 
--> rand.rs:2:9 
    | 
2 |  use rand::Rng; 
    |   ^^^^^^^^^ 
+0

Beachten Sie, dass dies eine Anfängerfrage ist. Dies ist (nach einer "Hallo Welt") die zweite Sache, die ich ausprobiert habe. –

Antwort

19

Im fernen Vergangenheit, die rand Kiste war Teil der Standardbibliothek, ist aber seit langem extracted to a crate. Diese Kiste sollte derjenige sein, die Sie verwenden:

Geben Sie einen Cargo.toml:

[package] 
name = "stackoverflow" 
version = "0.0.1" 
authors = ["A. Developer <[email protected]>"] 

[dependencies] 
rand = "0.3.15" # Or a newer version 

Dann wird Ihr Beispielcode funktioniert:

extern crate rand; 

use rand::Rng; 

fn main() { 
    let mut rng = rand::thread_rng(); 
    if rng.gen() { // random bool 
     println!("i32: {}, u32: {}", rng.gen::<i32>(), rng.gen::<u32>()) 
    } 
    let tuple = rand::random::<(f64, char)>(); 
    println!("{:?}", tuple) 
} 

Mit der Ausgabe:

$ cargo run 
    Running `target/debug/so` 
(0.310133, '\u{cd8fb}') 

$ cargo run 
    Running `target/debug/so` 
i32: 1568599182, u32: 2222135793 
(0.906881, '\u{9edc}') 

Warum waren? Diese nützlichen Funktionen wurden von stdlib entfernt?

Rust hat die Philosophie, so viel wie möglich in Kisten statt in die Standardbibliothek zu stellen. Dadurch kann jedes Codeelement wachsen und sich mit einer anderen Rate entwickeln als die Standardbibliothek. Außerdem kann der Code nicht mehr verwendet werden, ohne dass er für immer beibehalten wird. Ein übliches Beispiel ist das sequence of HTTP libraries in Python. Es gibt mehrere Pakete, die alle dasselbe auf verschiedene Arten tun und die Python-Betreuer müssen von ihnen halten, um Abwärtskompatibilität zu gewährleisten.

Kisten erlauben dieses besondere Ergebnis zu vermeiden. Wenn sich eine Kiste wirklich lange stabilisiert, kann sie sicher in die Standardbibliothek hinzugefügt werden.

+2

Können Sie bitte erklären, warum diese nützlichen Funktionen von stdlib entfernt werden? –

Verwandte Themen