Return Result in examples instead of unwrap

This commit is contained in:
Héctor Ramón Jiménez 2024-01-18 08:55:30 +01:00
parent 869737dadc
commit e8f2fbdaf0
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
3 changed files with 47 additions and 50 deletions

View file

@ -1,13 +1,14 @@
use rand::distributions::{Alphanumeric, Distribution};
use window_clipboard::Clipboard;
use winit::{
error::EventLoopError,
event::{ElementState, Event, KeyEvent, WindowEvent},
event_loop::EventLoop,
keyboard::Key,
window::WindowBuilder,
};
fn main() {
fn main() -> Result<(), EventLoopError> {
let mut rng = rand::thread_rng();
let data: String = Alphanumeric
@ -28,8 +29,7 @@ fn main() {
clipboard.write(data.clone()).unwrap();
event_loop
.run(move |event, elwt| match event {
event_loop.run(move |event, elwt| match event {
Event::WindowEvent {
event:
WindowEvent::KeyboardInput {
@ -53,5 +53,4 @@ fn main() {
} if window_id == window.id() => elwt.exit(),
_ => {}
})
.unwrap();
}

View file

@ -1,11 +1,12 @@
use window_clipboard::Clipboard;
use winit::{
error::EventLoopError,
event::{Event, WindowEvent},
event_loop::EventLoop,
window::WindowBuilder,
};
fn main() {
fn main() -> Result<(), EventLoopError> {
let event_loop = EventLoop::new().unwrap();
let window = WindowBuilder::new()
@ -16,8 +17,7 @@ fn main() {
let clipboard =
unsafe { Clipboard::connect(&window) }.expect("Connect to clipboard");
event_loop
.run(move |event, elwt| match event {
event_loop.run(move |event, elwt| match event {
Event::AboutToWait => {
println!("{:?}", clipboard.read());
}
@ -27,5 +27,4 @@ fn main() {
} if window_id == window.id() => elwt.exit(),
_ => {}
})
.unwrap();
}

View file

@ -1,11 +1,12 @@
use window_clipboard::Clipboard;
use winit::{
error::EventLoopError,
event::{Event, WindowEvent},
event_loop::EventLoop,
window::WindowBuilder,
};
fn main() {
fn main() -> Result<(), EventLoopError> {
let event_loop = EventLoop::new().unwrap();
let window = WindowBuilder::new()
@ -20,13 +21,11 @@ fn main() {
.write(String::from("Hello, world!"))
.expect("Write to clipboard");
event_loop
.run(move |event, elwt| match event {
event_loop.run(move |event, elwt| match event {
Event::WindowEvent {
event: WindowEvent::CloseRequested,
window_id,
} if window_id == window.id() => elwt.exit(),
_ => {}
})
.unwrap();
}