![](/img/trans.png)
[英]What is the most painless way to add a newline to the end of a String (or &str) in Rust?
[英]Add str no_std Rust
我已經開始編寫 no_std Rust 代碼,現在我正在嘗試添加字符串。
這是我的代碼:
#![no_std]
extern crate libc;
fn add_string(string: &str) {
const STRING1: &'static str = string;
const STRING2: &'static str = "String2";
const TEXT: &str = STRING1 + STRING2;
unsafe { libc::printf(TEXT.as_ptr() as *const _); }
}
#[no_mangle]
extern "C" fn main() -> ! {
add_string("String1 ");
}
在 no_std 程序中是否有任何好的庫可以做這樣的事情?
我的解決方案:
#![no_std]
extern crate alloc;
use alloc::{format,string::String};
fn add_string(string: &str) {
let res: String = format!("{} String2", string);
unsafe { libc::printf(res.as_ptr() as *const _); }
}
#[no_mangle]
extern "C" fn main() -> ! {
add_string("String1 ");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.