0

I am learning Rust. This is the code I'm running

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());

This the error I receive. Any help would be greatly appreciated.

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
3
  • 1
    Is your rust code in src/main.rs? or somewhere else? Commented Oct 23, 2021 at 17:32
  • 1
    looking at your terminal output again, it looks like Cargo.toml is in your user folder when it should be in play_ground/ Commented Oct 23, 2021 at 17:49
  • If your terminal is in \play_ground directory already, then you probably need cargo new --bin . instead Commented Oct 23, 2021 at 17:49

1 Answer 1

1

Your code have some syntax errors, should be something like this

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());
}

You can try it in playgound online https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=1c4f18874d42608a35b7e825aaf19619

Or in your computer you run:

  1. cargo init atest
  2. cd atest
  3. [code, notepad, or your prefer editor] src/main.rs "update main.rs
  4. cargo run
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.