如何在Rust中处理一串数字?
我有一个输入:
let b = String::from("1 2 4 5 6");
我的任务是将每个值的指数函数作为输出返回:
let output = "2.718281828459045 7.38905609893065 54.598150033144236 148.4131591025766 403.4287934927351";
我不知道如何解决这个任务。如何解析每个值并使用 exp 函数并发送结果字符串?
回答
这是成分:
- 拆分字符串:如何在 Rust 中拆分字符串?
- 将每个部分转换为
f64(或f32):https : //programming-idioms.org/idiom/146/convert-string-to-floating-point-number/1974/rust - 对每个数字取幂:https : //doc.rust-lang.org/std/primitive.f64.html#method.exp
- 将结果转换为字符串:如何将浮点数转换为字符串?
- 连接成单个字符串:https : //stackoverflow.com/a/41688369