首页 \ 问答 \ 这是从文件中读取行并将它们分解成Rust中的单词的正确方法吗?(Is this the right way to read lines from file and split them into words in Rust?)

这是从文件中读取行并将它们分解成Rust中的单词的正确方法吗?(Is this the right way to read lines from file and split them into words in Rust?)

编者按:本代码示例来自1.0之前的Rust版本,并且不具有语法上有效的Rust 1.0代码。 此代码的更新版本会产生不同的错误,但答案仍然包含有价值的信息。

我已经实现了下面的方法来从2维数据结构中的文件中返回单词:

fn read_terms() -> Vec<Vec<String>> {
    let path = Path::new("terms.txt");
    let mut file = BufferedReader::new(File::open(&path));
    return file.lines().map(|x| x.unwrap().as_slice().words().map(|x| x.to_string()).collect()).collect();
}

这是否是Rust中正确,习惯和有效的方式? 我想知道collect()需要经常调用,以及是否需要在这里调用to_string()来分配内存。 也许返回类型应该被定义为不同的地方,以更加地道和有效?


Editor's note: This code example is from a version of Rust prior to 1.0 and is not syntactically valid Rust 1.0 code. Updated versions of this code produce different errors, but the answers still contain valuable information.

I've implemented the following method to return me the words from a file in a 2 dimensional data structure:

fn read_terms() -> Vec<Vec<String>> {
    let path = Path::new("terms.txt");
    let mut file = BufferedReader::new(File::open(&path));
    return file.lines().map(|x| x.unwrap().as_slice().words().map(|x| x.to_string()).collect()).collect();
}

Is this the right, idiomatic and efficient way in Rust? I'm wondering if collect() needs to be called so often and whether it's necessary to call to_string() here to allocate memory. Maybe the return type should be defined differently to be more idiomatic and efficient?


原文:https://stackoverflow.com/questions/25581463
更新时间:2024-04-22 10:04

最满意答案

找出问题所在,我将分享我的见解:

addJavascriptInterface函数仅在调用BEFORE loadUrl / loadData函数时才适用。

在我的情况下 - 我期望addJavascriptInterface注入一个JS桥,但我从来没有重新加载WebView内容,因此从未主动注入。

重新加载WebView HTML内容后,桥接器按预期添加。


Figured out the problem, so I'll share my insights:

The addJavascriptInterface function applies only if called BEFORE a loadUrl / loadData function.

In my case - I expected addJavascriptInterface to inject a JS bridge, but I never reloaded the WebView content, so it was never actively injected.

After reloading the WebView HTML content, the bridge was added as expected.

相关问答

更多
  • 使用AppCompatImageView像这样:
  • 找出问题所在,我将分享我的见解: addJavascriptInterface函数仅在调用BEFORE loadUrl / loadData函数时才适用。 在我的情况下 - 我期望addJavascriptInterface注入一个JS桥,但我从来没有重新加载WebView内容,因此从未主动注入。 重新加载WebView HTML内容后,桥接器按预期添加。 Figured out the problem, so I'll share my insights: The addJavascriptInterfa ...
  • 由于几天后我处于类似情况,我认为以下内容可能有效(假设您在项目中使用BaseGameUtils,或者使用appCompat的任何模块) 在Android Studio中打开项目 双击使用appCompat的模块的build.gradle文件(对我来说,它是BaseGameUtils) 更改依赖关系如下(抱歉格式错误,我赶时间): 依赖{compile'com.android.support:appcompat-v7:22.0.0'compile'com.android.support:support-v4: ...
  • 您可以尝试使用ContextCompat: ContextCompat.getDrawable(yourContext, R.drawable.myDrawable).getConstantState(); You can try using ContextCompat: ContextCompat.getDrawable(yourContext, R.drawable.myDrawable).getConstantState();
  • 您应该尝试使用Android Studio,因为Eclipse的ADT插件不再处于积极的开发阶段。 在Android Studio中,您只需在build.gradle依赖项中添加一行compile 'com.nispok:snackbar:2.6.1' ,例如 dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:21.0.2' ...
  • 由于v21主题更改为包括: true 需要在v21的工具栏上修改边距,以考虑系统栏占用的空间。 dimens.xml 0dp dimens.xml(v21) -24dp 将margin和fitsSystemWi ...
  • 您可以针对不同的API级别使用不同的样式。 对于API <21,您将使用正常的res/values/styles.xml ,然后对于API 21+,您将拥有res/values-v21/styles.xml 。 如果设备运行API 21或更高版本,则它将使用-v21文件夹中的文件。 如果您只想设置值,那么只需将这些键命名为相同,然后对colors.xml执行相同的colors.xml 。 http://developer.android.com/guide/topics/resources/p ...
  • 我用当前信息填写了CUDA占用计算器文件: 计算能力:2.1 每块线程:961 每个线程的注册:34 共享内存:0 我的入住率为0% ,受寄存器数限制。 如果您将线程数设置为960,则占用率为63%,这就解释了它的工作原理。 尝试将寄存器的数量限制为32 ,并将线程数设置为1024 ,占用率为67%。 要限制寄存器的数量,请使用以下选项: nvcc [...] --maxrregcount=32 I filled the CUDA Occupancy calculator file with the cur ...
  • ~-(2 + "2") 0: 2 + "2" (连接)= "22" 1: -"22" (强制)= -22 2: ~-22 (按位NOT)= - ( - 22 + 1)= 21 按位注意任何数字x的产量 - (x + 1)。 例如,〜-5产生4。 ~-(2 + "2") 0: 2 + "2" (Concatenation) = "22" 1: -"22" (Coercion) = -22 2: ~-22 (Bitwise NOT) = -(-22 + 1) = 21 Bitwise NOTing any ...
  • 看起来您正在使用的某些功能(player.playVideo())在移动设备中被禁用。 在我的情况下,在某些Android设备中使用player.playVideo()后,即使点击了播放器,视频也无法播放 https://developers.google.com/youtube/iframe_api_reference?hl=zh-TW#Mobile_considerations 自动播放和脚本播放 在某些移动浏览器(例如Chrome和Safari)中,HTML5元素仅允许在用户交互(例如点击播放器)启动 ...

相关文章

更多

最新问答

更多
  • 带有简单redis应用程序的Node.js抛出“未处理的错误”(Node.js with simple redis application throwing 'unhandled error')
  • 高考完可以去做些什么?注意什么?
  • Allauth不会保存其他字段(Allauth will not save additional fields)
  • Flask中的自定义中止映射/异常(Custom abort mapping/exceptions in Flask)
  • sed没有按预期工作,从字符串中间删除特殊字符(sed not working as expected, removing special character from middle of string)
  • 怎么在《我的世界》游戏里面编程
  • .NET可移植可执行文件VS .NET程序集(.NET Portable Executable File VS .NET Assembly)
  • 搜索字符串从视图中键入两个字段的“名字”和“姓氏”组合(Search Strings Typed from View for Two Fields 'First Name' and 'Last Name' Combined)
  • 我可以通过配置切换.Net缓存提供程序(Can I switch out .Net cache provider through configuration)
  • 在鼠标悬停或调整浏览器大小之前,内容不会加载(Content Does Not Load Until Mouse Hover or Resizing Browser)
  • 未捕获的TypeError:auth.get不是函数(Uncaught TypeError: auth.get is not a function)
  • 如何使用变量值创建参数类(How to create a parameter class with variant value)
  • 在std :: deque上并行化std :: replace(Parallelizing std::replace on std::deque)
  • 单元测试返回Connection对象的方法(Unit Test for a method that returns a Connection object)
  • rails:上传图片时ios中的服务器内部错误(rails: server internal error in ios while uploading image)
  • 如何在Android中构建应用程序警报[关闭](How build an application Alarm in Android [closed])
  • 以编程方式连接到Windows Mobile上的蓝牙耳机(Programmatically connect to bluetooth headsets on Windows Mobile)
  • 在两个不同的SharedPreference中编写并获得相同的结果(Writing in two different SharedPreference and getting the same result)
  • CSS修复容器和溢出元素(CSS Fix container and overflow elements)
  • 在'x','y','z'迭代上追加数组(Append array on 'x', 'y', 'z' iteration)
  • 我在哪里可以看到使用c ++源代码的UML方案示例[关闭](Where I can see examples of UML schemes with c++ source [closed])
  • SQL多个连接在与where子句相同的表上(SQL Multiple Joins on same table with where clause)
  • 位字段并集的大小,其成员数多于其大小(Size of bit-field union which has more members than its size)
  • 我安装了熊猫,但它不起作用(I installed pandas but it is not working)
  • Composer - 更改它在env中使用的PHP版本(Composer - Changing the version of PHP it uses in the env)
  • 使用JavaFX和Event获取鼠标位置(Getting a mouse position with JavaFX and Event)
  • 函数调用可以重新排序(Can function calls be reordered)
  • 关于“一对多”关系的NoSQL数据建模(NoSQL Data Modeling about “one to many” relationships)
  • 如何解释SBT错误消息(How to interpret SBT error messages)
  • 调试模式下的Sqlite编译器错误“初始化程序不是常量”(Sqlite compiler errors in Debug mode “initializer is not a constant”)