Add str no_std Rust

1.7k views Asked by At

I've been starting to write no_std Rust code and right now I'm trying to add strings.

This is my code:

#![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 ");
}

Are there any good libraries for doing something like this in a no_std program?

1

There are 1 answers

1
Atomic Gamer On

Read the Docs

The answer is always there

My Solution:

#![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 ");
}