习惯性与Rust中的堕落相匹配(Idiomatic match with fall-through in Rust)
我是Rust的新手,但作为Haskell的粉丝,我非常感谢Rust的
match
方式。 现在我面临着一个罕见的情况,我确实需要堕落 - 从某种意义上说,我希望所有匹配的几个重叠的案例都能被执行。 这有效:fn options(stairs: i32) -> i32 { if stairs == 0 { return 1; } let mut count: i32 = 0; if stairs >= 1 { count += options(stairs - 1); } if stairs >= 2 { count += options(stairs - 2); } if stairs >= 3 { count += options(stairs - 3); } count }
我的问题是,这是否是Rust中的惯用语或是否有更好的方法。
编辑:上下文是Cracking the Coding Interview的一个问题:“一个孩子正在爬楼梯,有n个步骤,可以一步跳1步,2步或3步。 实施一种方法来计算孩子爬楼梯的可能方式。“
I’m new to Rust, but as a fan of Haskell, I greatly appreciate the way
match
works in Rust. Now I’m faced with the rare case where I do need fall-through – in the sense that I would like all matching cases of several overlapping ones to be executed. This works:fn options(stairs: i32) -> i32 { if stairs == 0 { return 1; } let mut count: i32 = 0; if stairs >= 1 { count += options(stairs - 1); } if stairs >= 2 { count += options(stairs - 2); } if stairs >= 3 { count += options(stairs - 3); } count }
My question is whether this is idiomatic in Rust or whether there is a better way.
Edit: The context is a question from Cracking the Coding Interview: “A child is running up a staircase with n steps and can hop either 1 step, 2 steps, or 3 steps at a time. Implement a method to count how many possible ways the child can run up the stairs.”
原文:https://stackoverflow.com/questions/42509526
最满意答案
您可以使用
CGPDFDocumentCreateWithProvider
:如果你的NSData引用是
pdfData
那么:CFDataRef myPDFData = (CFDataRef)pdfData; CGDataProviderRef provider = CGDataProviderCreateWithCFData(myPDFData); CGPDFDocumentRef pdf = CGPDFDocumentCreateWithProvider(provider);
但我想你只是想在网页视图中显示它而上述并不是你需要的。
但是,如果您已经拥有该URL,则不会更容易存储URL而不是pdf。
此外,如果您有数据,可以暂时将其写入文档目录中的文件,然后使用Web视图加载此URL。
即;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"New1.pdf"]; NSURL *fileURL = [NSURLRequest requestWithURL:appFile]; [data writeToURL:fileURL atomically:YES];
接着 :
NSURLRequest *request = [NSURLRequest requestWithURL:fileURL]; [webView loadRequest:request];
如果要从文档目录中删除文件,可以使用:
[[NSFileManager defaultManager] removeItemAtURL:fileUrl error:nil];
删除您创建的临时pdf。
You can use
CGPDFDocumentCreateWithProvider
:If your NSData reference is
pdfData
then:CFDataRef myPDFData = (CFDataRef)pdfData; CGDataProviderRef provider = CGDataProviderCreateWithCFData(myPDFData); CGPDFDocumentRef pdf = CGPDFDocumentCreateWithProvider(provider);
But I guess you just want to display it in a web view and the above is not what you need.
However if you already have the url wouldn't it be easier to store the URL rather than the pdf as such.
Also if you have the data you can write it into a file inside your documents directory temporarily and then use web view to load this url.
i.e;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"New1.pdf"]; NSURL *fileURL = [NSURLRequest requestWithURL:appFile]; [data writeToURL:fileURL atomically:YES];
and then :
NSURLRequest *request = [NSURLRequest requestWithURL:fileURL]; [webView loadRequest:request];
And if you want to remove the file from the documents directory you could use:
[[NSFileManager defaultManager] removeItemAtURL:fileUrl error:nil];
to remove the temporary pdf you created.
相关问答
更多-
在5.3.3更新之后,Unity将JsonUtility添加到它们的API中。 忘记所有的第三方库,除非你正在做更复杂的事情。 JsonUtility比其他Json库快。 更新到Unity 5.3.3或更高版本,然后尝试下面的解决方案。 JsonUtility是一个轻量级的API。 只支持简单的类型。 它不支持Dictionary等集合。 List是个例外。 它支持List和List数组! 如果您需要序列化Dictionary或执行其他操作,而不是简单地序列化和反序列化简单数据类型,请使用第三方API。 否 ...
-
这对我有用: @Test public void serializeTest() throws Exception { KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA"); KeyPair keyPair = kpg.generateKeyPair(); ByteArrayOutputStream b = new ByteArrayOutputStream(); ObjectOutputStream o = ...
-
您可以使用CGPDFDocumentCreateWithProvider : 如果你的NSData引用是pdfData那么: CFDataRef myPDFData = (CFDataRef)pdfData; CGDataProviderRef provider = CGDataProviderCreateWithCFData(myPDFData); CGPDFDocumentRef pdf = CGPDFDocumentCreateWithProvider(provider); ...
-
如何对数据库进行序列化和反序列化Perl数据?(How can I serialize and deserialize Perl data to/from database?)[2022-04-03]
答案取决于您需要序列化的数据类型。 Native:有打包/解包,对于普通的数字数组,你可以用逗号等加入......任何简单的本机方法都是特定于域的,不适用于通用数据。 Storable是一种很好的标准方法。 还有其他人(我听说过FreeseThaw但从未使用过)。 您还可以执行YAML或JSON格式化 - 为了进行比较,请参阅最近的StackOverflow问题 ,该问题涉及通过IO :: Socket :: INET发送数据数组(这也涉及序列化)。 进一步的编码需要取决于您的序列化以及您对数据的处理方式。 ... -
对于DataContractSerializer ,它默认使用字母顺序; 并期望XML元素按照这个顺序到达; 乱序元素通常会被丢弃。 看起来XML已经被接收,并且你不能改变XML元素的顺序,你可以使用如下的Order属性 。 [DataContract(Name = "Response", Namespace = "")] public class MyResponse { [DataMember(Order =1)] public string ClientID { get; set; } ...
-
使用BinaryFormatter序列化和反序列化对象图(Serialize and Deserialize object graph using BinaryFormatter)[2023-12-20]
这里有一些代码会做你想做的事情(我想) - 但我要问 - 你为什么要序列化为这样的字符串? 如果类足够简单以序列化为字符串,请使用更易于处理的XML序列化程序; 如果你想将它序列化到磁盘,二进制文件将其写入文件,如果它很复杂并且你将它序列化以进行传输 - 请考虑使用protobuf-net之类的东西。 我认为你的问题的关键是你正在尝试使用ASCII编码 - 我正在使用Base64编码。 无论如何 - 这里(我只是猜测你的Search类!) class Program { [Serializable ... -
std::string的实现包括指向堆上实际字符串内容的指针。 因此, sizeof(string)只是指针加上一些字节。 如果要编写字符串,则必须自己编写内容 for (auto i = list.begin(); i != list.end(); ++i) { os.write(i->c_str(), i->size() + 1); } 当你再读回来时,你必须寻找终止的NUL字节。 或者,您可以像保存列表一样保存字符串的长度 for (auto i = list.begin(); i != l ...
-
我的问题是两件事: 我排除了“* .class”。 没有理由在我的情况下这样做,因此我删除了它。 我更改了deserialize部分以获取我的数据字符串,我使用nio读取数据,然后将其与StringBuilder连接,如下例所示: try { List
res = Files.readAllLines(Paths.get(myObject), StandardCharsets.UTF_8); StringBuilder anotherTest = new Stri ... -
如何完全序列化/反序列化RSAParameters对象(How to completely serialize/deserialize RSAParameters object)[2022-08-22]
以下代码可用于使用您自己选择的序列化程序序列化和反序列化RSAParameters对象。 using System; using System.Runtime.Serialization; using System.Security.Cryptography; [Serializable] public class RSAParametersSerializable : ISerializable { private RSAParameters _rsaParameters; publi ... -
如何使用Woodstox StAX 2序列化/反序列化XML中的类(How do I serialize / deserialize a class in XML with Woodstox StAX 2)[2022-04-19]
JAXB是将Java对象序列化为XML的方式。 以下内容将帮助您入门: http://wiki.eclipse.org/EclipseLink/Examples/MOXy/GettingStarted JAXB实现 该标准有几种实现方式: EclipseLink MOXy (我是技术主管) Metro (参考实现,包含在Java SE 6中) JaxMe Woodstox StAX 2 JAXB接受许多输入/输出格式,包括StAX。 验证 使用Unmarshaller将XML转换为对象,并使用Marshal ...