当前位置:网站首页>Rust实现斐波那契数
Rust实现斐波那契数
2022-04-22 13:11:00 【SmallTeddy】
1、本地生成目录
// 创建目录
cargo new fib
// 进入目录
cd fib

2、编写代码
在src/main.rs文件,根据用户输入的数字n,返回第n个斐波那契数。
use std::io;
fn main() {
fib();
}
fn fib() {
let mut n = String::new();
let (mut a, mut b) = (0, 1);
let mut sum;
println!("Please input a number");
io::stdin().read_line(&mut n).expect("Failed to read line");
let n: i32 = n.trim().parse().expect("Please type a number!");
for _i in 0..n {
sum = a + b;
a = b;
b = sum;
}
println!("fib number is {}", a);
}
3、运行结果
使用cargo run直接编译并返回结果。

版权声明
本文为[SmallTeddy]所创,转载请带上原文链接,感谢
https://blog.csdn.net/SmallTeddy/article/details/124334678
边栏推荐
- How to become an open source database developer?
- Graph search of obstacles in far planner
- Mpu6050-dmp cannot read data
- Digital business cloud electronic bidding system solution - standardize the political procurement process and improve work efficiency
- Ros2 - use of parameters
- 树莓派压缩备份
- The R language uses the merge function to left join two dataframe data
- How to batch delete worksheets in Excel
- 数商云家电商城系统解决方案,优化电器商城采购供应链管理,减低库存提升资金利用率
- Study notes - Digital Factory 4.21
猜你喜欢

学习笔记——数字化工厂 4.21

The system solution of digital commerce cloud home appliance mall optimizes the procurement and supply chain management of the electrical appliance mall, reduces inventory and improves capital utiliza

Stm32cubemx redirects printf output to serial port

2022年上半年软考备考冲刺攻略

It's rare to make a profit or a flash in the pan. Daphne can't learn from the Antarctic

Excel表格中如何批量删除工作表

Altium Designer导出Gerber文件的一般步骤

calloc和realloc

ROS2——参数的使用

Far planner之 障碍物的图搜索
随机推荐
From construction to governance, the industry's first white paper on microservice governance technology was officially released (including a free download link)
Rsync remote synchronization
销量大腰斩,岚图无蓝图
Ros2 - teach you how to write a service
Sprintf format string
ROS2——什么是接口
分块——优雅的暴力
MATLAB中的数据从double类型强制转化为uint8其舍入用的是四舍五入,附MATLAB基本数据类型
redis 更新升级版本
How to batch delete worksheets in Excel
柔性印刷电路板(PCB)的设计方法、类型等详细解析
There are four ways to traverse the mat class matrix elements of OpenCV
The compatibility certification of ecological Wanli database and Yixin technology has been completed
With the help of digital business cloud real estate industry procurement platform solution, realize simple, intelligent and compliant procurement
R语言使用merge函数对两个dataframe数据进行左连接(Left join)
Drawing violin picture with R language geom_ Violin, how to add additional points geom_ point? geom_ violin + geom_ boxplot + geom_ Point combination
树莓派压缩备份
R语言绘制小提琴图geom_violin,如何添加额外的点geom_point?geom_violin + geom_boxplot + geom_point组合使用
500 Internal Server Error错误补充
ROS机器人学习——麦克纳姆轮运动学解算