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

I tried following that same article, but I couldn't fix it - could you share your process?

I added the palette dependency in the Cargo.toml file like so:
palette = { version = "0.5", features = [ "named" ]}

Here is the section in its entirety:
[dependencies]
nannou = "0.18.1"
palette = { version = "0.5", features = [ "named" ]}

The named feature is important to enable you to pick colours based on name. I hope you will get it up and running.

Cheers!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.