如何在嵌入式平台中将 u32 数据转换为 &str?

How do I convert u32 data to &str in an embedded platform?

我想在嵌入式 Rust 中将 u32 整数数据转换为字符串,但问题是在嵌入式中我们不能使用 std 代码,那么有什么办法可以做到这一点吗?

let mut dist = ((elapsed as f32 / mono_timer.frequency().0 as f32 * 1e6) / 2.0) / 29.1;
let dist = dist as u32;
let data = String::from("data:");
data.push_str(dist);

找到答案

use core::fmt::Write;
use heapless::String;
use heapless::consts::*;

fn foo(){
    let dist = 100u32;
    let mut data = String::<U32>::from("data:");
    
    // `write` for `heapless::String` returns an error if the buffer is full,
    // but because the buffer here is 32 bytes large,the u32 will fit with a 
    //lot 
    // of space left.
    let _=write!(data,"{}", dist);
}