Variable not found in scope

I am getting an error, that variable is not in scope.

That is sad to hear.

Perhaps if you posted the relevant code here for people to see then someone could advise how to fix it.

2 Likes
#![feature(proc_macro_hygiene, decl_macro)]

#[macro_use] extern crate rocket;
#[macro_use] extern crate serde_derive;
extern crate serde;
extern crate rocket_contrib;
extern crate postgres;


use postgres::{Connection, TlsMode};


use rocket_contrib::templates::Template;

#[derive(Serialize)]
struct IndexContext 
{
u: String,
p: String,
r: String,
o: String,
d: String,
s: String,
z: String,
f: String,
m: String
}

#[get("/")]
fn index() -> Template
{
struct Select {
aa: String,
ab: String,
ac: String,
ad: String,
ae: String,
af: String,
ag: String,
ah: String,
ai: String
} 
    let conn = Connection::connect("postgresql://postgres:postgres@localhost/postgres", TlsMode::None).unwrap();

    for row in &conn.query("SELECT aa,ab,ac,ad,ae,af,ag,ah,ai FROM xyz WHERE id=1", &[]).unwrap() {
        let qwe = Select {
        
        aa: row.get(0),
        ab: row.get(1),
        ac: row.get(2),
        ad: row.get(3),
        ae: row.get(4),
        af: row.get(5),
        ag: row.get(6),
        ah: row.get(7),
        ai: row.get(8),
        };
let context = IndexContext
{
u: qwe.aa,
p: qwe.ab,
r: qwe.ac,
o: qwe.ad,
d: qwe.ae,
s: qwe.af,
z: qwe.ag,
f: qwe.ah,
m: qwe.ai,
};
 println!("Found person {} {} {} {} {} {} {} {} {}", context.u,context.p,context.r,context.o,context.d,context.s,context.z,context.f,context.m);
    }
return Template::render("index", &context);
}






fn main() {
    rocket::ignite()
        .mount("/", routes![index])
        .attach(Template::fairing()).launch();
}

//main.rs

#![feature(proc_macro_hygiene, decl_macro)]

#[macro_use] extern crate rocket;
#[macro_use] extern crate serde_derive;
extern crate serde;
extern crate rocket_contrib;
extern crate postgres;


use postgres::{Connection, TlsMode};


use rocket_contrib::templates::Template;

#[derive(Serialize)]
struct IndexContext 
{
u: String,
p: String,
r: String,
o: String,
d: String,
s: String,
z: String,
f: String,
m: String
}

#[get("/")]
fn index() -> Template
{
struct Select {
aa: String,
ab: String,
ac: String,
ad: String,
ae: String,
af: String,
ag: String,
ah: String,
ai: String
} 
    let conn = Connection::connect("postgresql://postgres:postgres@localhost/postgres", TlsMode::None).unwrap();

    for row in &conn.query("SELECT aa,ab,ac,ad,ae,af,ag,ah,ai FROM xyz WHERE id=1", &[]).unwrap() {
        let qwe = Select {
        
        aa: row.get(0),
        ab: row.get(1),
        ac: row.get(2),
        ad: row.get(3),
        ae: row.get(4),
        af: row.get(5),
        ag: row.get(6),
        ah: row.get(7),
        ai: row.get(8),
        };
let context = IndexContext
{
u: qwe.aa,
p: qwe.ab,
r: qwe.ac,
o: qwe.ad,
d: qwe.ae,
s: qwe.af,
z: qwe.ag,
f: qwe.ah,
m: qwe.ai,
};
 println!("Found person {} {} {} {} {} {} {} {} {}", context.u,context.p,context.r,context.o,context.d,context.s,context.z,context.f,context.m);
    }
return Template::render("index", &context);
}






fn main() {
    rocket::ignite()
        .mount("/", routes![index])
        .attach(Template::fairing()).launch();
}

//error

That code shown is such a mess I'm guessing it is not what you have actually tried to compile. Or that you have other errors before the one shown above.

You have defined "context" within a "for" loop. Variable defined in a for loop, or any other scope surrounded by "{" and "}" are not visible outside that scope. So context does not exist when you come to use it later.

What should I do to use "context" outside the for loop???

Create it outside the loop.

Code with rustfmt:

#![feature(proc_macro_hygiene, decl_macro)]

#[macro_use]
extern crate rocket;
#[macro_use]
extern crate serde_derive;
extern crate postgres;
extern crate rocket_contrib;
extern crate serde;

use postgres::{Connection, TlsMode};

use rocket_contrib::templates::Template;

#[derive(Serialize)]
struct IndexContext {
    u: String,
    p: String,
    r: String,
    o: String,
    d: String,
    s: String,
    z: String,
    f: String,
    m: String,
}

#[get("/")]
fn index() -> Template {
    struct Select {
        aa: String,
        ab: String,
        ac: String,
        ad: String,
        ae: String,
        af: String,
        ag: String,
        ah: String,
        ai: String,
    }
    let conn = Connection::connect(
        "postgresql://postgres:postgres@localhost/postgres",
        TlsMode::None,
    )
    .unwrap();

    for row in &conn
        .query("SELECT aa,ab,ac,ad,ae,af,ag,ah,ai FROM xyz WHERE id=1", &[])
        .unwrap()
    {
        let qwe = Select {
            aa: row.get(0),
            ab: row.get(1),
            ac: row.get(2),
            ad: row.get(3),
            ae: row.get(4),
            af: row.get(5),
            ag: row.get(6),
            ah: row.get(7),
            ai: row.get(8),
        };
        let context = IndexContext {
            u: qwe.aa,
            p: qwe.ab,
            r: qwe.ac,
            o: qwe.ad,
            d: qwe.ae,
            s: qwe.af,
            z: qwe.ag,
            f: qwe.ah,
            m: qwe.ai,
        };
        println!(
            "Found person {} {} {} {} {} {} {} {} {}",
            context.u,
            context.p,
            context.r,
            context.o,
            context.d,
            context.s,
            context.z,
            context.f,
            context.m
        );
    }
    return Template::render("index", &context);
}

fn main() {
    rocket::ignite()
        .mount("/", routes![index])
        .attach(Template::fairing())
        .launch();
}

One solution, depending on what you want to do, is to move the return inside the for loop.

1 Like

If I create it outside the loop, then how can I assign values to the structure?

Can we assign values inside the loop to a structure outside the loop?

Playground to start with. If you don't understand what is going on there, I'd suggest dropping this project for a while and looking through the basics.

"context" lifetime has expired. I think we need to specify lifetime to "context"..

Error about variables not in context have nothing to do with lifetimes. It's a syntax error. The same as if you write asdfkjhsfksdkkjlklj in your code, and it'll be just random garbage that the compiler doesn't recognize.

If you're generating a code in a macro, it can be caused by macro hygiene — macros are not allowed to see any variables outside of the macro, unless the variable was passed in as an argument to the macro.

That is not how lifetimes work. Please read up on lifetimes and ownership,

I remember reading this good quote from @withoutboats:

1 Like