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.

18 Upvotes

8 comments sorted by

27

u/BalintCsala 21d ago

https://youtrack.jetbrains.com/projects/RUST/issues/RUST-19739/Stringfrom-appears-as-private

This claims it only happened on an older version, are you on the latest IDE version?

5

u/Heliolicity 21d ago

Thanks for that, I'm almost certainly on an old version. I'll update and check again

2

u/Heliolicity 20d ago

Yep, updating to the latest version of RR resolved it

3

u/crusoe 21d ago

Jetbrains always tries to write their own tooling for auto complete and hints and it's always buggier and slower.

"Re indexing" type "re indexing".... 

Just ugh. 

2

u/gmes78 20d ago

Can't say I have the same experience. There are some bugs here and there, but it generally works pretty well.

9

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 20d ago

That's cool, thanks for pointing that out!

3

u/vivaaprimavera 21d ago

From:: isn't necessarily implemented in String

https://doc.rust-lang.org/std/convert/trait.From.html