添加到struct/impl的生存期使模块无法解决
我的项目是以这种方式建立的:添加到struct/impl的生存期使模块无法解决,struct,rust,lifetime,Struct,Rust,Lifetime,我的项目是以这种方式建立的: src/lib/rustic_io.rs src/lib/server.rs src/main.rs main.rs #[path="lib/rustic_io.rs"] mod rustic_io; fn main() { // Setup information (Server is not started yet) let mut server = rustic_io::listen("127.0.0.1", 1338); //
src/lib/rustic_io.rs
src/lib/server.rs
src/main.rs
main.rs
#[path="lib/rustic_io.rs"]
mod rustic_io;
fn main() {
// Setup information (Server is not started yet)
let mut server = rustic_io::listen("127.0.0.1", 1338);
// Event with data to listen for
server.on("connection", |data: &str| {
// Do stuff in here with data
});
// Start server
server.start();
}
mod server;
/*
* Returns a new Server to start WebSocket Protocol
*/
pub fn listen(ip_addr: &str, port_num: u16) -> server::Server {
server::Server::new(String::from_str(ip_addr), port_num)
}
pub struct Server<'srv> {
ip: String,
port: u16,
events: Vec<Event<'srv>>
}
impl &'a Server {
// Constructs a Server object
pub fn new<'a>(ip_addr: String, port_num: u16) -> Server<'a> {
Server {
ip: ip_addr,
port: port_num,
events: Vec::new()
}
}
pub fn on(&self, event: &str, execute: |data: &str|) {
// Do some stuff
}
pub fn start(&self) {
// Start up all the things
}
}
mod event {
pub struct Event<'a> {
name: String,
execute: |data: &str|:'a
}
}
乡村风格
#[path="lib/rustic_io.rs"]
mod rustic_io;
fn main() {
// Setup information (Server is not started yet)
let mut server = rustic_io::listen("127.0.0.1", 1338);
// Event with data to listen for
server.on("connection", |data: &str| {
// Do stuff in here with data
});
// Start server
server.start();
}
mod server;
/*
* Returns a new Server to start WebSocket Protocol
*/
pub fn listen(ip_addr: &str, port_num: u16) -> server::Server {
server::Server::new(String::from_str(ip_addr), port_num)
}
pub struct Server<'srv> {
ip: String,
port: u16,
events: Vec<Event<'srv>>
}
impl &'a Server {
// Constructs a Server object
pub fn new<'a>(ip_addr: String, port_num: u16) -> Server<'a> {
Server {
ip: ip_addr,
port: port_num,
events: Vec::new()
}
}
pub fn on(&self, event: &str, execute: |data: &str|) {
// Do some stuff
}
pub fn start(&self) {
// Start up all the things
}
}
mod event {
pub struct Event<'a> {
name: String,
execute: |data: &str|:'a
}
}
server.rs
#[path="lib/rustic_io.rs"]
mod rustic_io;
fn main() {
// Setup information (Server is not started yet)
let mut server = rustic_io::listen("127.0.0.1", 1338);
// Event with data to listen for
server.on("connection", |data: &str| {
// Do stuff in here with data
});
// Start server
server.start();
}
mod server;
/*
* Returns a new Server to start WebSocket Protocol
*/
pub fn listen(ip_addr: &str, port_num: u16) -> server::Server {
server::Server::new(String::from_str(ip_addr), port_num)
}
pub struct Server<'srv> {
ip: String,
port: u16,
events: Vec<Event<'srv>>
}
impl &'a Server {
// Constructs a Server object
pub fn new<'a>(ip_addr: String, port_num: u16) -> Server<'a> {
Server {
ip: ip_addr,
port: port_num,
events: Vec::new()
}
}
pub fn on(&self, event: &str, execute: |data: &str|) {
// Do some stuff
}
pub fn start(&self) {
// Start up all the things
}
}
mod event {
pub struct Event<'a> {
name: String,
execute: |data: &str|:'a
}
}
指向此语句:server::server::new(字符串::from_str(ip_addr),port_num)
我觉得在函数内部的各种变量上使用生命周期时,我对生命周期有着相当好的理解,但在设计自己的结构和实现时,我不知道生命周期是什么。当实现使用生命周期时,为什么服务器模块不再可解析?我觉得我正在以完全相反的方式进行这项工作,并且使生命周期说明符异常…这不是一个生命周期问题。我能够使用以下代码编译您的代码:
impl&'a服务器{…}
的语法无效。将其更改为impl{…}
Vec>
您还可以更改
fn newThanks以获取今天的所有帮助:)