我试着做this piece of Ruby个:

def color=(color)
  @color = color
  any_bar = UDPSocket.new
  any_bar.connect HOSTNAME, @port
  any_bar.send @color, 0
  any_bar.close
end

如果没有bind(),我想不出其他办法来达到initialize a UdpSocket from the Rust API documentation.

推荐答案

我会try ::bind("0.0.0.0:0")-这应该让O/S为您 Select 一个IP/端口.这可能足以让一个临时套接字发送一个简单的数据报.

Note: this is what happens too when using sendto() on an unbound UDP socket too, e.g. using the fd returned from the socket() system call without calling bind() - the O/S allocates an IP/port to send your datagram from.

Rust相关问答推荐

当为a Self:IntoIterator设置trait bind `时,获取`a T `不是迭代器"&'"<'>&'

如何找到一个数字在二维数组中的位置(S)?

MutexGuard中的过滤载体不需要克隆

带参考文献的 rust 元组解构

我如何制作一个变异迭代器来锁定内部数据直到删除?

如何修复数组中NewType导致的运行时开销

零拷贝按步骤引用一段字节

在Rust中克隆源自INTO_ITER()的迭代器的成本?

链表堆栈溢出

sha256 摘要仅适用于 &*

trait 对象指针的生命周期

如何将 C++ 程序链接到 Rust 程序,然后将该 Rust 程序链接回 C++ 程序? (cpp -> rust -> cpp)

使用 Rust 从 Raspberry Pi Pico 上的 SPI 读取值

从 HashMap>, _> 中删除的生命周期问题

类型判断模式匹配panic

如何将 Rust 中的树状 struct 展平为 Vec<&mut ...>?

使用部分键从 Hashmap 中检索值

实现不消费的迭代器

覆盖类型的要求到底是什么?为什么单个元素元组满足它?

如何创建动态创建值并向它们返回borrow 的工厂?