Cannot find function `from_str` in module `named`

Hello Rustaceans, I am new to Rust and I am learning it via the excellent nannou creative coding crate and the community tutorials. However, I have run into a problem that appears to be trivial, but I simply can not find a solution. In Ben Lovy's tutorial, hi converts a String into a Srgb colour type using the named type, in other words: use nannou::color::named;

The code that does not work look like this:
named::from_str(&c.to_string()).unwrap()

The error reads:
from_str [...] not found in named

However if I go to the definition of named I find from_str at the bottom of the page:
#[cfg(feature = "named_from_str")]
pub fn from_str(name: &str) -> Option<::Srgb> {
COLORS.get(name).cloned()
}

So it there, but the compiler can not "see" it, right? How do I make the compiler "see" this code?

Thank you very much for your help in advance!

Best,
Rikard

The #[cfg(...)] thing above the from_str() function is for conditional compilation. The idea is that the compiler will only try to compile that function when the named_from_str feature is enabled in your Cargo.toml.

The Cargo book has a section on Features if you want to learn more, but the idea is to update your Cargo.toml to include the list of enabled features. It'll look something like this:

[dependencies]
nannou = { version = "...", features = ["named_from_str"] }

That said, I can't see a definition for the named_from_str feature in nannou's Cargo.toml, so maybe you are using an older version or there's something funny going on with the way they do feature flags?

1 Like

Thank you Michael!

It turned out to be more complex. The palette crate crate was removed from nannou 0.18 to begin with. crates.io turned out to be very useful. And, just as you write, I had to switch in the from string feature for palette.

Cheers!

1 Like