I'm new and i need help with code

Hello, I am new to programming in Rust and I would like to write some code from c ++ just in Rust but I have no idea how I could do it, could anyone help? Thank you right away and don't get into the matter with this code :smiley:


using namespace std;

string PIN;

int main()
cout << "Welcome to our bank!" << endl;
cout<<"enter PIN:";
cin >> PIN;

    cout<<"correct PIN";
    cout<<"uncorrenct PIN";

return 0;


Please format your code by surrounding it with backticks (```) as per the pinned topic.

If you haven't read the Rust Programming Language book, it does a pretty good job at introducing the language, including how to print out and read in text.

1 Like

The code does three things, so let's break it down:

  • print the welcome message;

  • ask for input;

  • check if the input is equal to "1729".

Most of these can be solved by reading the Programming a Guessing Game chapter of the book. Here's an additional tip: Rust's output is line-buffered by default, so to print a partial line and wait for input, flush is necessary. For now, you can use this convenience wrapper (admittedly crude), which is similarly to Python's input:

use std::io::{self, prelude::*};

fn input(message: &str) -> io::Result<String> {
    let stdout = io::stdout();

    let mut stdout = stdout.lock();
    write!(stdout, "{}", message)?;

    let mut input = String::new();
    io::stdin().read_line(&mut input)?;

    if input.ends_with('\n') {

You can use it like:

let pin = input("Enter PIN: ");

By the way, here's my version of the C++ code that you posted:

#include <iostream>
#include <string>

int main()
    std::cout << "Welcome to our bank!\n"
              << "Enter PIN: ";

    std::string input;
    std::getline(std::cin, input);

    if (input == "1729") {
        std::cout << "Correct PIN.\n";
    } else {
        std::cout << "Incorrect PIN.\n";

I got rid of using namespace std;, which is discouraged at global scope, and the global variable. I also used std::getline, as opposed to >>, which stops at whitespace.

What have you tried so far? Have you read The Rust Programming Language or some other introduction to Rust? You might also want to check the Rosetta Code entries for Conditionals, print statements, or user input. Rosetta Code is awesome when you already know a language and want to know how a particular piece of code is implemented in something else!

I don't want to sound too critical or be jumping the gun here, but if you are asking a question it's usually a good idea to mention what you've tried or what your thought process is. That way, as well as saving time by letting us know what did/didn't work, you indicate to others that you've invested time to solve your problem and aren't just asking them to do your work for free.

We can always give you the final answer... but if you don't first try to figure it out by checking documentation or googling it, you will miss out on the chance to learn and have that really satisfying "aha!" moment when you finally solve a problem and everything clicks into place :slight_smile:


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.