0
J'apprends Rust, et j'essaye de faire un serveur WebSocket qui calcule le Fibonacci pour n et envoie le résultat arrière. Je reçois l'erreur:ws-rs: E0271r: expected(), trouvé enum `std :: result :: Result`
expected(), found enum `std::result::Result`
Voici mon code (avec commentaires):
extern crate ws;// add websocket crate
extern crate num;// add num crate (to handle big numbers)
extern crate regex;// regex crate
use ws::listen;
use num::bigint::BigUint;
use num::traits::{Zero, One};
use std::env;
use std::mem::replace;// used to swap variables
fn main() {
let re = regex::Regex::new("[0-9]+").unwrap();// regex to check if msg is a number.
listen("0.0.0.0:8080", |conn| {// this is where the arrows in the error message points to
move |msg| {
if re.is_match(msg) {// check if message matches the regex
let num: i64 = msg.parse().unwrap();// set num to the msg as an integer
conn.send(ws::Message::Text(fib(num).to_string()));// create a new ws message with the Fib of num
}
}
}).unwrap();
}
fn fib(n: i64) -> BigUint {// fibonacci function
let mut f0 = Zero::zero();
let mut f1 = One::one();
for _ in 0..n {
let f2 = f0 + &f1;
f0 = replace(&mut f1, f2);
}
f0
}