【问题标题】:Rust Error failed to parse manifest no targets specifiedRust 错误未能解析清单未指定目标
【发布时间】:2021-10-23 17:15:33
【问题描述】:

我正在学习 Rust。这是我正在运行的代码

Struct Object {
    width: u32,
    height:u32,
}

impl Object {
    fn area(&slef) --> u32 {

    }

    fn new(width: u32, height: u32) --> Object {
        Object {
            width,
            height,
        }
    }
}


fn main() {
    let o = Object {
        width: 35,
        height: 55,
    };
    
    let obj = Object ::new(57,83);

    println!("{}x{} with area: {}", o.width, o.height, o.area());
    println!("{}x{} with area: {}", obj.width, obj.height, obj.area());

这是我收到的错误。任何帮助将不胜感激。

PS C:\Users\Jessica\play_ground> cargo new play_ground --bin
warning: compiling this new package may not work due to invalid workspace configuration

failed to parse manifest at `C:\Users\Jessica\Cargo.toml`

Caused by:
  no targets specified in the manifest
  either src/lib.rs, src/main.rs, a [lib] section, or [[bin]] section must be present
     Created binary (application) `play_ground` package
PS C:\Users\Jessica\play_ground> cargo run
error: failed to parse manifest at `C:\Users\Jessica\Cargo.toml`

Caused by:
  no targets specified in the manifest
  either src/lib.rs, src/main.rs, a [lib] section, or [[bin]] section must be present
PS C:\Users\Jessica\play_ground

【问题讨论】:

  • 你的 rust 代码在src/main.rs 中吗?还是其他地方?
  • 再次查看您的终端输出,看起来Cargo.toml 在您的用户文件夹中,而它应该在play_ground/
  • 如果你的终端已经在\play_ground目录下,那么你可能需要cargo new --bin .来代替

标签: rust rust-cargo


【解决方案1】:

你的代码有一些语法错误,应该是这样的

struct Object {
    width: u32,
    height:u32
}

impl Object {
    fn area(&self) -> u32 {
        self.width * self.height
    }

    fn new(width: u32, height: u32) -> Self {
        Self {
            width,
            height,
        }
    }
}


fn main() {
    let o = Object {
        width: 35,
        height: 55,
    };
    
    let obj = Object ::new(57,83);

    println!("{}x{} with area: {}", o.width, o.height, o.area());
    println!("{}x{} with area: {}", obj.width, obj.height, obj.area());
}

你可以在playgound在线试一试https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=1c4f18874d42608a35b7e825aaf19619

或者在你的电脑上运行:

  1. 货物初始化测试
  2. cd 测试
  3. [代码、记事本或您喜欢的编辑器] src/main.rs "update main.rs
  4. 货物运行

【讨论】:

    猜你喜欢
    • 2016-09-26
    • 2014-03-03
    • 2015-11-17
    • 2014-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多