rust-lang / www.rust-lang.org

The home of the Rust website
https://www.rust-lang.org
Apache License 2.0
362 stars 287 forks source link

compile error for `ferris-says` demo in the learn/get-started page #1942

Closed hartvon closed 2 months ago

hartvon commented 2 months ago

What needs to be fixed?

Hello, I just started learning rust, it seems like a compile error when I execute cargo run for the ferris-says demo in the learn/get-started page. Please verify it.

The source code:

use ferris_says::say; // from the previous step
use std::io::{stdout, BufWriter};

fn main() {
    let stdout = stdout();
    let message = String::from("Hello fellow Rustaceans!");
    let width = message.chars().count();

    let mut writer = BufWriter::new(stdout.lock());
    say(&message, width, &mut writer).unwrap();
}

The error log:

$ cargo run
   Compiling hello-rust v0.1.0 (/home/htvon/codeSet/rust/hello-rust)
error[E0308]: mismatched types
  --> src/main.rs:10:9
   |
10 |     say(&message, width, &mut writer).unwrap();
   |     --- ^^^^^^^^ expected `&[u8]`, found `&String`
   |     |
   |     arguments to this function are incorrect
   |
   = note: expected reference `&[u8]`
              found reference `&String`
note: function defined here
  --> /home/htvon/.cargo/registry/src/index.crates.io-6f17d22bba15001f/ferris-says-0.2.1/src/lib.rs:86:8
   |
86 | pub fn say<W>(input: &[u8], max_width: usize, writer: &mut W) -> Result<()>
   |        ^^^

For more information about this error, try `rustc --explain E0308`.
error: could not compile `hello-rust` (bin "hello-rust") due to 1 previous error

Page(s) Affected

The get-started page: https://www.rust-lang.org/learn/get-started

Suggested Improvement

It works when I use the example code in https://crates.io/crates/ferris-says

senekor commented 2 months ago

From the instructions, ferris-says should be at version 0.3.1:

[dependencies]
ferris-says = "0.3.1"

You are using 0.2.1, which can be seen on this line of your output:

--> /home/htvon/.cargo/registry/src/index.crates.io-6f17d22bba15001f/ferris-says-0.2.1/src/lib.rs:86:8

How did you add the dependency to get the outdated version?

Updating the version should fix your issue.

hartvon commented 2 months ago

From the instructions, ferris-says should be at version 0.3.1:

[dependencies]
ferris-says = "0.3.1"

You are using 0.2.1, which can be seen on this line of your output:

--> /home/htvon/.cargo/registry/src/index.crates.io-6f17d22bba15001f/ferris-says-0.2.1/src/lib.rs:86:8

How did you add the dependency to get the outdated version?

Updating the version should fix your issue.

Thanks for your quick reply! This was indeed my fault, I copied the ferris-says = "0.2" in How to use the library chapter from https://crates.io/crates/ferris-says site into the Cargo.toml file, which caused the incorrect version of ferris-says.