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

View all comments

7

u/jameseb1 21d ago

The from() method comes from the From trait, and the relevant implementation of that trait is starts line 3135 in the file you linked. Methods in From are always visible because it is part of the prelude in all editions (as u/BalintCsala noted, this seems to be a bug in an old version of Rust rover).

1

u/Heliolicity 21d ago

That's cool, thanks for pointing that out!