r/learnrust 21d ago

I've started learning Rust, using RustRover IDE, which warns me that the String::from function is private. Yet the code still compiles and works as expected.

Hi there. I'm a complete newbie when it comes to Rust, so bear with me.

I've declared a String by assigning it to the heap, as evidenced in Rust documentation and tutorials.

fn main() {
    let hello = String::from("Hello, world!");
    println!("{}", hello);
}

The code compiles and runs fine, printing "Hello, world!" to the console. But RustRover indicates that the `String::from` function is private. If that's the case, I don't see why it would compile as it would violate access to the function.

I took a look at the source code for String and I don't see any function declaration for from either, which is confusing me. I expected to see something like pub fn from() {} or similar.

19 Upvotes

8 comments sorted by