Hi - brand new Rust programmer here (sorry if this is an incredibly stupid question + the code is weird + etc... ).
I have the following struct (focus on the commented line "panic!("from struct");")...
pub struct DbPgsqlMain
{
SOME VARIABLES HERE
}
impl DbPgsqlMain
{
pub fn new(hostname:String, port:u16, schema:String, username:String, userpwd:String) -> DbPgsqlMain
{
let conn_parms:String = format!(
"host={} dbname={} user={} password={} port={} connect_timeout=60 keepalives=1 keepalives_idle=60",
hostname, schema, username, userpwd, port);
printmsg(0, false, format!("Trying to connect to the DB \"{}\" on host \"{}\".", &schema, &hostname));
match postgres::Client::connect(conn_parms.as_str(),postgres::NoTls)
{
Ok(connection) =>
{
printmsg(1, false, format!("Connected successfully to the DB \"{}\" on host \"{}\".", &schema, &hostname));
return DbPgsqlMain {
SOME PARAMETERS HERE
};
}
Err(e) =>
{
printmsg(1, true, format!("Connection attempt to DB \"{}\" on host \"{}\" failed with the error\"{}\".", &schema, &hostname, e));
//panic!("from struct");
}
};
}
}
... and I have the following function (focus on the line "{ panic!("from func"); }"):
pub fn printmsg<S>(idt:usize, is_error:bool, msg:S)
where S: Into<String>
{
let mut output:String = String::from("TODO INSERT HERE TIMESTAMP ");
//Add leading indentation
output.push_str(" ".repeat(idt).as_str());
if is_error == true
{ output.push_str("ERROR - "); }
output.push_str(msg.into().as_str());
println!("{}", output);
if is_error == true
{ panic!("from func"); }
}
The above will NOT compile unless I uncomment the line "panic!("from struct");" in the struct method "new()" of DbPgsqlMain => why? Respectively, what should I do to be able to avoid the extra "panic!("from struct");"-call in the struct?
I understand on a high level the compilation error message "expected struct DbPgsqlMain
, found ()
" as I'm not returning anything in that ("Err()") portion of code, but anyway as I hardcode "is_error" to "true" in the function call the endresult through the function is the same (calling "panic!") as calling "panic!" directly... .
Thanks