Is there anyway to convert type u32 to &str not &String

I tried converting u32 to str but i cant find a way to do it then i tried to convert u32 to &String then &String to &str but that doesnt work too.

&String and &str are both borrowed values, so there needs to be an owned value they borrow from. That would look something like this:

let owned = 3.to_string();
let borrowed = &owned;
// use `borrowed` in the rest of your program

Most of the time &String to &str is done automatically when you call a function or print it out, you normally don't need an explicit conversion. If you do, you can use as_str().

See Understanding Ownership - The Rust Programming Language for more details on ownership and String vs &String vs &str.

1 Like

I suggest reading through the following article, my favourite one about &str-vs-String in rust:

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.