fnfib(n:u64)->u64{letmut a =0;letmut b =1;for _ in0..n {let c = a + b;a = b;b = c;}a
}fnmain(){let n =10;let result_list:Vec<String>=(1..n+1).map(|i|fib(i).to_string()).collect();let result = result_list.join(",");println!("{}", result);}
计算表达式(加减乘除)
usestd::io;usestd::num::ParseIntError;fnparse_expression(expression:&str)->Result<i32,ParseIntError>{letmut result =0;letmut current_number =0;letmut operator ='+';for token in expression.split_whitespace(){match token {"+"=> operator ='+',"-"=> operator ='-',"*"=> operator ='*',"/"=> operator ='/',_ =>{current_number =match token.parse::<i32>(){Ok(num)=> num,Err(e)=>returnErr(e),};match operator {'+'=> result += current_number,'-'=> result -= current_number,'*'=> result *= current_number,'/'=> result /= current_number,_ =>{},}}}}Ok(result)}fnmain(){println!("请输入表达式(例如:1 + 2 * 3):");letmut input =String::new();io::stdin().read_line(&mut input).expect("读取输入失败");let expression = input.trim();matchparse_expression(expression){Ok(result)=>println!("计算结果为:{}", result),Err(e)=>println!("解析表达式时出错:{}", e),}}